Jump to content

preg_match_all, obtain part of the table?


godsent

Recommended Posts

 

<tr class="category">

<td class="category"><a href="javascript:nothing()" class="tooltiphelp nounder nolink" title="From camping to luxury hotels, costs are for one person and assume double occupancy.">Accommodation</a><sup><a class="tooltiphelp" href="javascript:nothing()" title="Calculated by averaging the cost of each item. For example, the cost of transportation is by trip, and not by day.">1</a></sup></td>
<td class="value">
 
<span class="symbol">$</span>
 
<span class="curvalue">103.98</span> </td>
</tr><tr class="category">
<td class="category"><a href="javascript:nothing()" class="tooltiphelp nounder nolink" title="All meals and snacks over the course of one day.">Food</a><sup><a class="tooltiphelp" href="javascript:nothing()" title="Calculated with a daily average. For example the cost of food is a total daily average (the total of all meals and snacks for one day), and not per each meal.">2</a></sup></td>
<td class="value">
 
<span class="symbol">$</span>
 
<span class="curvalue">39.62</span> </td>
</tr>

 

Say I wanted to obtain words from the above code: accommodation and food. Before both of them there is '>' symbol and after both of them there is </a><sup><a class="tooltiphelp"

 

So I am trying to get words in between code: > WORD </a><sup><a class="tooltiphelp"

preg_match_all('/>(.*)<\</a><sup><a class=/i',$str, $match);

But It seems that I do not understand that I am doing very well, and this regex syntax is very puzzling to me. How should the syntax look like?

Link to comment
https://forums.phpfreaks.com/topic/278853-preg_match_all-obtain-part-of-the-table/
Share on other sites

Don't use regular expressions to sift through HTML. Use a real HTML parser like DOMDocument, which then lets you use DOMXPath. Like

$dom = new DOMDocument();
$dom->loadHTML($str);

$xpath = new DOMXPath($dom);
$nodes = $xpath->query("//tr[@class='category']/td[@class='category']/a");

foreach ($nodes as $node) {
        echo $node->textContent, "\n";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.