I don't speak English very well. So, if i'll make some mistake please sorry.
On the site i have a div box with some information about game:
<span class="noteline">Developer:</span>
<span class="subline">Gameloft</span>
<span class="noteline">Genre:</span>
<span class="subline">Racing/Arcade</span>
<span class="noteline">Release year:</span>
<span class="subline">2010</span>
I need to get the information between <span class="noteline"> and it's closing tag </span>
preg_match("/\<span\sclass=\"subline\"\>(.*)<\/span\>/imsU", $source, $matches);
the solution above works fine but it only gets the "subline" with text "gameloft";
but i need also sublines that have text Racing/Arcade and 2010;
Maybe something like this (that doesn't work);
for developer = preg_match("/*(\<span\sclass=\"subline\"\>){1}*(.*)*(<\/span\>){1}*/imsU", $source, $matches);
for genre = preg_match("/*(\<span\sclass=\"subline\"\>){2}*(.*)*(<\/span\>){2}*/imsU", $source, $matches);
something like this..
Anyway. Thanks for any help.
An alternative to regexps would be to use phpQuery or QueryPath, which simplifies it to:
foreach ( qp($source)->find("span.subline") as $span ) {
print $span->text();
}
Regular expressions are not appropriate to parse HTML. They are difficult to get right and they always break in edge cases.
I don't know if there's an easier way but this should work with the markup you describe:
<?php
$fragment = '<span class="noteline">Developer:</span>
<span class="subline">Gameloft</span>
<span class="noteline">Genre:</span>
<span class="subline">Racing/Arcade</span>
<span class="noteline">Release year:</span>
<span class="subline">2010</span>';
libxml_use_internal_errors(TRUE);
$dom = new DOMDocument();
$dom->loadHTML($fragment);
$xml = simplexml_import_dom($dom);
libxml_use_internal_errors(FALSE);
foreach($xml->xpath("//span[@class='subline']") as $item){
echo (string)$item . PHP_EOL;
}
This assumes class="subline" so it'll fail with multiple classes. (New to Xpath so improvements welcome.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With