regular expression to get contents from HTML tags

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
amir
Forum Contributor
Posts: 287
Joined: Sat Oct 07, 2006 4:28 pm

regular expression to get contents from HTML tags

Post by amir »

Hello,
How can I get contents from HTML tags?

Code: Select all

<table width="40%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="left" class="css_text">Name</td>
    <td>Aamir</td>
  </tr>
  <tr>
    <td align="left" class="css_email">Email</td>
    <td>abc@mail.com</td>
  </tr>
  <tr>
    <td align="left" class="css_phone">Phone</td>
    <td>1234567890</td>
  </tr>
</table>
I would like to get it as

Code: Select all

$name  = 'Aamir';
$email  = 'abc@mail.com'; 
$phone = '1234567890';
Thanks in Advance.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It will take a bit more than simply a regular expression to generate individual variables.

What have you tried as far as the regular expression goes?
amir
Forum Contributor
Posts: 287
Joined: Sat Oct 07, 2006 4:28 pm

Post by amir »

actually I dont know how to get these values and I want to know how can I do this with or without regular expression?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There have been many threads on extracting data from within HTML tags. If you're unwilling to attempt an expression, I can't help you.
amir
Forum Contributor
Posts: 287
Joined: Sat Oct 07, 2006 4:28 pm

Post by amir »

I am sorry to hurt you but here is what I wanted

Code: Select all

preg_match_all("|<[^>]+>(.*)</[^>]+>|U", 
	   "<td align=\"left\" class=\"css_text\">Name</td><td>Aamir</td>
	   <td align=\"left\" class=\"css_email\">Email</td><td>abc@mail.com</td>
	   <td align=\"left\" class=\"css_phone\">Phone</td><td>1234567890</td>", 
	   $out, PREG_SET_ORDER);
	echo $out[0][0].' = '.$out[1][0]."<br>";
	echo $out[2][0].' = '.$out[3][0]."<br>";
	echo $out[4][0].' = '.$out[5][0]."<br>";
Thanks for your support =)
Post Reply