[英]Extract string of HTML code with PHP
此表達式僅獲取尖括號之間的值> <當它們是數字時。 我想在任何情況下得到它們。
function GetProducts($file){
$regex = "|class=\"producto\"[^>]+>([0-9]*)</[^>]+>|U";
if(!is_file($file)) return false;
preg_match_all($regex,file_get_contents($file), $result);
foreach($result[1] as $key =>$value) $result[$key] = (int) $value;
return $result;
}
這是我的HTML代碼:
<a class="producto" href="ver.asp?id=4013">A86028</a></span><!-- /a --></td></tr>
<a class="producto" href="ver.asp?id=4014">1027C</a></span><!-- /a --></td></tr>
<a class="producto" href="ver.asp?id=4014">5611 4020</a></span>
<!-- /a --></td></tr>
<a class="producto" href="ver.asp?id=4014">396-4185</a></span>
<!-- /a --></td></tr>
<a class="producto" href="ver.asp?id=4014">834006-5-7</a></span>
<!-- /a --></td></tr>
<a class="producto" href="ver.asp?id=4014">5601GR 4325GR</a></span>
<!-- /a --></td></tr>
<a class="producto" href="ver.asp?id=4014">2182CR(2)</a></span>
<!-- /a --></td></tr>
<a class="producto" href="ver.asp?id=4014">1458-54-63-55</a></span>
<!-- /a --></td></tr>
我想要的輸出是:
Array ([1] => 1027 [2] => 5611 [3] => 5396 [4] => 834006 [5] => 5601 [6] => 2182 [7] => 1458)
這可能有用,但正如人們所說用正則表達式解析html是有問題的。
# class="producto"[^>]+>([^<]*)</[^>]+>
class="producto" [^>]+ >
( [^<]* )
</ [^>]+ >
你在這里要求一個純正則表達式,但它不是解析HTML的正確工具 。
function _matcher ($m, $str) {
if (preg_match('/^\d+/', $str, $matches))
$m[] = $matches[0];
return $m;
}
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
foreach ($xpath->query('//a[@class="producto"]') as $link) {
$vals[] = $link->nodeValue;
}
print_r(array_reduce($vals, '_matcher', array()));
輸出( 工作演示 )
Array
(
[0] => 1027
[1] => 5611
[2] => 396
[3] => 834006
[4] => 5601
[5] => 2182
[6] => 1458
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.