By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,161 Members | 2,002 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,161 IT Pros & Developers. It's quick & easy.

how to perg_match between <tr>and </tr> or <td>ot</td> in php

P: 7
I want to match some particular text between <tr>and </tr> or <td>and </td>....
in PHP.

like below............
<table><tr>
CATEGORY:
<td><font face="Verdana" size="1" color="#A000A0">
Wedding

Accessories
</font></td>
<td><font face="Verdana" size="1" color="#A000A0">
Wedding

Crèche
</font></td>
<td>
<font face="Verdana" size="1" color="#A000A0">
Wedding

Gifts
</font></td>


<table>
<tr>
WORLDWIDE:

FOR INTERNATIONAL SUPPLIERS CLICK HERE
<td><font face="Verdana" size="2" color="#A000A0"><b>
Australia<b/>
</font></td> <td><font face="Verdana" size="2" color="#A000A0"><b>Granada Malta</b></font></td>


BUT Want to match only category table(categories have same font style ,face ,size and color).

and also want to match internation suppliers(pls look countries have <b></b>tags between <font></font> tags
Nov 22 '06 #1
Share this Question
Share on Google+
5 Replies


TheMadMidget
P: 98
I don't know of a direct way other than a string comparision.
[PHP]
$start = strpos($str, "string"); // use this to get right before the first tag
$start = strpos($str, "string", $start); //(optional) use this to get right before the first tag if the line above only get to two tags before, use as many as neccessary
$start = strpos($str, "<td>", $start) + 4;
$end = strpos($str, "</td>", $start) ;
$length = $end - $start;
$contents = substr($str, $start, $length);
[/PHP]

It's a little crude, but works.
Nov 22 '06 #2

100+
P: 143
Sorry, do I understand that you want the text between the tags?

The the expression should be something along the lines of...

preg_match("/<td>(.*?)<\/td>/", $stringToSearch, $matches);

This will capture all the text between the <td></td> tags. It will be in matches[1].

You would need to read the html file into a string, using file_get_contents(). You could run a regular expression to capture just the table you want. But you should change your HTML, place the table titles in the <caption> tag, then you can modify the expression above to capture between caption tags.
Nov 23 '06 #3

P: 7
hi all,


thanks,but not work.i want to a text like

Wedding
Accessories

Wedding
Crèche

only Between Category tr's or td's..pls look this have <font> tags...so easy to match using this font but do not know the code...
Nov 23 '06 #4

TheMadMidget
P: 98
after you get what in between the <td> / <tr> tag use
$contents = strip_tags($contents);
This does exactly what it sounds like, it strips all tags so everything between a < and a > goes away, all of them. You can also chose to keep certain ones. For futher reference: http://us3.php.net/strip_tags.
Cheers,
Happy Thanksgiving
Nov 23 '06 #5

P: 17
after you get what in between the <td> / <tr> tag use
$contents = strip_tags($contents);
This does exactly what it sounds like, it strips all tags so everything between a < and a > goes away, all of them. You can also chose to keep certain ones. For futher reference: http://us3.php.net/strip_tags.
Cheers,

Happy Thanksgiving
hai try this code..i think this is working perfectly

preg_match_all('/\<tr\>(.*?)\<\/tr>/is', $content, $matches);
Nov 28 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.