I have multiple PHP strings (10-100k characters each) and want to get exact elements with their children from them.
Example:
$bbc_string = file_get_contents('http://www.bbc.co.uk/');
echo $bbc_string;
Gives 90544 characters and displays the whole page. But who needs the whole page, including navigation, footer etc.?
What if I want to display only, let’s say:
<div id="news_container">
everything in it
</div>
Or:
<h2 id="worldService_title">
everything in it
</h2>
How to filter or parse $bbc_string output to show what I want?
I think regex is the solution, I’ve found this pattern:
preg_match('/<div id=\"".$id."\">(.*?)</div>/', $string, $matches);
But it doesn’t work and is supposed to work only for divs not all elements.
Any PHP/regex ninjas around?
You should use DOM to parse HTML.