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
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";
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.