0

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

2 Answers2

0

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í
Harshal
  • 3,562
  • 9
  • 36
  • 65
  • tried it, but that echoes "Maršov nad Metují" - I wanted to get the cell values, not the value of the first cell – user3790770 Aug 04 '14 at 14:49
  • basically the values inside the cells are changing and I want to get these values based on the name given in the second cell. In this case, the "Maršov nad Metují" has values A, 90, etc. and I would like to load these into a string – user3790770 Aug 04 '14 at 14:51
0
preg_match_all("/<td.*?>(.+?)<\/td>/is", $html, $matches);
$result = $matches[1];
array_shift($result);
array_shift($result);
print implode(', ', $result);
  • Well, this doesnt do what it is supposed to either, but I have managed to find a way of doing it right, thx anyway :-) – user3790770 Aug 04 '14 at 15:08