Reputation: 83
I am trying to extract a piece of text from an HTML using PHP command preg_match. Ive successfully parsed the HTML into a variable, but now I got stuck with extracting the right piece of information - probably because I am a bit confused by the syntax of preg_match.
So basically, here is a piece of the HTML I am interested in:
...<tr >
<td >Metuje</td>
<td ><a href="./detail_stanice/307158.html" >Maršov nad Metují</a></td>
<td >A</td>
<td >90</td>
<td >120</td>
<td >150</td>
<td >cm</td>
<td >04.08. 14:20</td>
<td >31</td>
<td >0.53</td>
<td ><img src="./img/ldown.png" width="15" /></td>
</tr>...
What I need is to find this particular row in the table (which contains couple of other rows), so basically I need to search for the name "Maršov nad Metují" in the second cell and then, extract the values of the subsequent cells on that row into a string, in other words in this particular case I would like to have a string with values A, 90, 120, etc. until the end of the row.
On the website there are then other rows with the exact same format just with different values, so I would then use the same syntax to extract values for rows with different names in the second cell.
I have tried it myself, but I was not able to get the right output. I tried something like this, but this does not solve the problem, I know I have to somehow implement the cell TD commands, but unfortunately I wasnt able to get it right in this particular case.:
preg_match("/Maršov nad Metují(.*?)\<\/tr/", $html, $results);
Any help is very much appreciated. Thanks
Upvotes: 0
Views: 443
Reputation: 66
preg_match_all("/<td.*?>(.+?)<\/td>/is", $html, $matches);
$result = $matches[1];
array_shift($result);
array_shift($result);
print implode(', ', $result);
Upvotes: 0
Reputation: 3622
Try this :
<?php
$info = '<tr ><td >Metuje</td><td ><a href="./detail_stanice/307158.html" >Maršov nad Metují</a></td><td >A</td><td >90</td><td >120</td><td >150</td><td >cm</td><td >04.08. 14:20</td><td >31</td><td >0.53</td><td ><img src="./img/ldown.png" width="15" /></td></tr>';
preg_match('/<a href="(.*)" >(.*)</Ui',$info,$result);
print_r($result[2]);// MarÅ¡ov nad MetujÃ
Upvotes: 0