我有一个包含许多<li> .. </li>
的HTML字符串。我想从每个<li> ...</li>
集合中解析以下数据:
1: call.php?category=fruits&fruitid=123456
2: mango season
3: http://imagehosting.com/images/fru_123456.png
我使用preg_match_all函数获取第一个值,但是如何获取第二个和第三个值?如果有人能演示一下如何获取第二个和第三个值,那就太好了。谢谢!
php:
preg_match_all('/getit(.*?)detailFruit/', $code2, $match);
var_dump($match);
// iterate the new array
for($i = 0; $i < count($match[0]); $i++)
{
$code3=str_replace('getit(\'', '', $match[0]);
$code4=str_replace('&\',detailFruit', '', $code3);
echo "<br>".$code4[$i];
}
示例 <li> ..</li>
数据:
<li><a id="FR123456" onclick="setFood(false);setSeasonFruitID('123456');getit('call.php?category=fruits&fruitid=123456&',detailFruit,false);">mango season</a><img src="http://imagehosting.com/images/fru_123456.png">
</li>
编辑:我现在使用DOM,得到了2和3的值,如何使用DOM获取第一个值?
libxml_use_internal_errors(true);
$dom = new DOMDocument;
$dom->loadHTML($code2);
$xpath = new DOMXPath($dom);
// Empty array to hold all links to return
$result = array();
//Loop through each <li> tag in the dom
foreach($dom->getElementsByTagName('li') as $li) {
//Loop through each <a> tag within the li, then extract the node value
foreach($li->getElementsByTagName('a') as $links){
$result[] = $links->nodeValue;
echo $result[0] . "\n";
}
$imgs = $xpath->query("//li/img/@src");
foreach ($imgs as $img) {
echo $img->nodeValue . "\n";
}
}
echo $links->getAttribute('onclick');
获取属性,然后你可以使用正则表达式,但我不知道解析JS的方法。 - chris85