如果你在一個大的字符串有:
<?php
$str = '<a href="ABC.jpg">Hello</a> <a href="DEF.jpg">Bye</a>';
$expr = '/<a[^>]*? href=\"(?<url>[^\"]+)\"[^>]*?>(?<text>.*?)<\/a>/';
echo preg_match_all($expr, $str, $amatches);
echo '<br><br>';
print_r($amatches);
?>
您還可以使用:
preg_match_all($expr, $str, $amatches, PREG_SET_ORDER);
可能符合好(我OFEN喜歡這種方式)如果你在有
在循環中指定字符串和句柄,使用preg_match代替。 Tis將只返回第一個,然後你循環。
編輯:這裏是你的例子,根據要求。
<?php
$str = '<a onmouseover="dgsa.sm(this)" onmouseout="dgsa.hm();" href="http://www.cnn.com/testpage.html#page_mostview">test titles</a>';
$expr = '/<a[^>]*? href=\"(?<url>[^\"]+)\"[^>]*?>(?<text>.*?)<\/a>/';
echo preg_match_all($expr, $str, $amatches, PREG_OFFSET_CAPTURE);
echo '<br><br>';
print_r($amatches);
?>
使用'DOMDocument'解析HTML並從中提取值。 – Ryan 2012-08-09 00:53:47
http://stackoverflow.com/a/1732454/374601 – 2012-08-09 00:55:56
@erty