如何使用PHP解析<li>标签内的<a name>和<img src>?

3

我有一个包含许多<li> .. </li>的HTML字符串。我想从每个<li> ...</li>集合中解析以下数据:

   1: call.php?category=fruits&amp;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('&amp;\',detailFruit', '', $code3);
echo "<br>".$code4[$i];
}

示例 <li> ..</li> 数据:

<li><a id="FR123456" onclick="setFood(false);setSeasonFruitID('123456');getit('call.php?category=fruits&amp;fruitid=123456&amp;',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";
}
}

2
不要使用正则表达式进行解析,应使用解析器。请参考:https://dev59.com/EnA65IYBdhLWcg3w4C-j?rq=1。 - chris85
谢谢回复。根据您的建议,我使用了DOM,但是如何使用DOM获取第一个值,以便我可以使用DOM获取每个集合的所有三个值? - user1788736
你的例子中的“第一个值”是什么? - chris85
我指的是 onclick 内的这个值: call.php?category=fruits&fruitid=123456 - user1788736
1
你可以使用 echo $links->getAttribute('onclick'); 获取属性,然后你可以使用正则表达式,但我不知道解析JS的方法。 - chris85
1个回答

1
有趣的问题 :-) 以下解决方案使用了DOMDocument/SimpleXML的组合,以便轻松获取2和3的值。由于您的HTML片段已损坏,因此使用了DomDocument。要从JavaScript内容中获取链接(值1),使用了简单的正则表达式:
~getit\('([^']+)'\)~
# search for getit( and a singlequote literally
# capture everything up to (but not including) a new single quote
# this is saved in the group 1

下面可以找到完整的步骤说明(显然我编造了banana这部分):
<?php
$html = '<ul>
<li><a id="FR123456" onclick="setFood(false);setSeasonFruitID(\'123456\');getit(\'call.php?category=fruits&amp;fruitid=123456&amp;\',detailFruit,false);">mango season</a><img src="http://imagehosting.com/images/fru_123456.png"></li>
<li><a id="FR7890" onclick="setFood(false);setSeasonFruitID(\'7890\');getit(\'call.php?category=fruits&amp;fruitid=7890&amp;\',detailFruit,false);">bananas</a><img src="http://imagehosting.com/images/fru_7890.png"></li>
        </ul>';

$dom = new DOMDocument;
$dom->strictErrorChecking = FALSE;
$dom->loadHTML($html);
$xml = simplexml_import_dom($dom);

# xpath to find list items
$items = $xml->xpath("//ul/li");

$regex = "~getit\('([^']+)'\)~";

# loop over the items
foreach ($items as $item) {
    $title = $item->a->__toString();
    $imgLink = $item->img["src"];

    $jsLink = $item->a["onclick"];

    preg_match_all($regex, $jsLink, $matches);
    $jsLink = $matches[1][0];

    echo "Title: $title, imgLink: $imgLink, jsLink: $jsLink\n";
    // output: Title: mango season, imgLink: http://imagehosting.com/images/fru_123456.png, jsLink: call.php?category=fruits&fruitid=123456&
    //         Title: bananas, imgLink: http://imagehosting.com/images/fru_7890.png, jsLink: call.php?category=fruits&fruitid=7890&
}

?>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接