Question

My html output source is something like this

<td><span class="bookdetailtitle">ISBN</span></td>
                            <td>:</td>
                            <td>9788172338299</td>

I need only 9788172338299 to be printed. If the above code is in same line, it prints properly. But since there are new lines and tabs, I'm not getting the output. I tried replacing /i with /s, but not working. I want preg_match to match the string regardless of new lines or tabs and print the desired output.

Here is my code:

$page2='<td><span class="bookdetailtitle">ISBN</span></td>
                            <td>:</td>
                            <td>9788172338299</td>';

preg_match('/<td><span class="bookdetailtitle">ISBN<\/span><\/td><td>:<\/td><td>(.*)<\/td>/s', $page2, $keywords);
echo $keywords_out = $keywords[1];
Was it helpful?

Solution

If you need just number, something like this?

$page2='<td><span class="bookdetailtitle">ISBN</span></td>
                            <td>:</td>
                            <td>9788172338299</td>';

preg_match('/<td>+[0-9]+<\/td>/', $page2, $keywords);
print_r($keywords); 

http://phpfiddle.org/main/code/43j-t8b

P.S. many will say - don't use regex for parsing html. I agree. :)

I would do something like this:

$page=explode('<td>',$page2);
print_r($page[3]);

http://phpfiddle.org/main/code/buf-95c

Edit: to get rid of last td -> print_r(strip_tags($page[3]));

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top