通过preg_match_all获取图像网址

时间:2013-07-15 16:02:22

标签: php image html-parsing preg-match-all

<p class="my-image my-image-zoom">
    <div id="wrap" style="top:0px;z-index:9999;position:relative;"><a href="http://img3.mysite.com/image.jpg" class="cd-zoom" id="prozoom" rel="adjustX: 5, zoomWidth:526, zoomHeight:440, adjustY:-1" style="position: relative; display: block;">
    <img width="400" height="440" id="image" src="http://img3.mysite.com/image.jpg" style="display: block;"></a><div class="mousetrap" style="background-image: url(http://www.mysite.com/); z-index: 999; position: absolute; width: 400px; height: 440px; left: 0px; top: 0px; cursor: move;"></div></div> 
</p>

我试过

preg_match_all('/product-image-zoom">(.*?)/s',$url,$sav);
print_r($sav);

我想剪辑图像源。最终我从获得类名的所有值开始。我发现我的代码无效。任何人都可以帮助我获得图像的来源吗?

3 个答案:

答案 0 :(得分:1)

虽然建议不要使用正则表达式来解析html,但我建议你这样做:

    $dom = new DOMDocument('1.0', 'utf-8');
    @$dom->loadHTML($html);

    $xpath_query = "//img";
    $xpath = new DOMXPath($dom);
    $xpath_query_results = $xpath->query($xpath_query);

    foreach($xpath_query_results as $result)
    {
        $src = $result->getAttribute('src');
        print_r($src);
    }

然后你可以针对正则表达式测试$src变量:)

答案 1 :(得分:1)

我建议不要使用正则表达式来解析HTML。这是另一种解决方案:

<?php

$html= '<p class="my-image my-image-zoom">
    <div id="wrap" style="top:0px;z-index:9999;position:relative;"><a href="http://img3.mysite.com/image.jpg" class="cd-zoom" id="prozoom" rel="adjustX: 5, zoomWidth:526, zoomHeight:440, adjustY:-1" style="position: relative; display: block;">
    <img width="400" height="440" id="image" src="http://img3.mysite.com/image.jpg" style="display: block;"></a><div class="mousetrap" style="background-image: url(http://www.mysite.com/); z-index: 999; position: absolute; width: 400px; height: 440px; left: 0px; top: 0px; cursor: move;"></div></div> 
    </p>';

$doc = new DOMDocument();
@$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$src = $xpath->evaluate("string(//img/@src)");

echo $src; //output: http://img3.mysite.com/image.jpg

?>

键盘:http://codepad.org/C4oKp4LI

希望这有帮助!

答案 2 :(得分:0)

不要使用正则表达式来解析HTML 。你不能用正则表达式可靠地解析HTML,你将面临悲伤和挫折。一旦HTML改变了您的期望,您的代码就会被破坏。有关如何使用已编写,测试和调试过的PHP模块正确解析HTML的示例,请参阅http://htmlparsing.com/php