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 wor开发者_如何学Ck);
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.)
Try this:
preg_match_all("/<span class=\"subline\".*span>/", $html, $matches);
preg_match_all("/<span class=\"noteline\".*span>/", $html, $matches);
I tried the above code this way:
<?php
$html = '<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>';
preg_match_all("/<span class=\"subline\".*span>/", $html, $matches1);
preg_match_all("/<span class=\"noteline\".*span>/", $html, $matches2);
print_r($matches1);
echo "<br>";
print_r($matches2);
?>
The output I got was this:
Array ( [0] => Array ( [0] => Gameloft [1] => Racing/Arcade [2] => 2010 ) )
Array ( [0] => Array ( [0] => Developer: [1] => Genre: [2] => Release year: ) )
精彩评论