drf97973 2013-07-17 14:02
浏览 40
已采纳

preg_match_all没有从php中的文件中获取图像src

I have page image.php

where images are kept in container like below :- Note: There are other Images outside container div too.. i just want images from container div.

    <!DOCTYPE html>
    <head>
        <title>Image Holder</title>
    </head>
    <body>
        <header>
        <a href="#"><img src="http://examepl.com/logo.png"></a>
            <div id="side">
                <div id="facebook"><img src="http://examepl.com/fb.png"></div>
                <div id="twiiter"><img src="http://examepl.com/t.png"></div>
                <div id="gplus"><img src="http://examepl.com/gp.png"></div>
            </div>
        </header>      
        <div class="container">
            <p>SOme Post</p>
            <img src="http://examepl.com/some.png" title="some image" />

            <p>SOme Post</p>
            <img src="http://examepl.com/some.png" title="some image" />

            <p>SOme Post</p>
            <img src="http://examepl.com/some.png" title="some image" />
        </div>
        <footer>
            <div id="foot">
                copyright &copy; 2013
            </div>
        </footer>
    </body>
</html>

and i am trying to fetch only image from my image.php file with preg_match_all, but it returns boolean(false) :(

my php code :-

<?php
$file = file_get_contents("image.php");
preg_match_all("/<div class=\"container\">(.*?)</div>/", $file, $match);
preg_match_all("/<img src=\"(.*?)\">/", $match, $images);

var_dump($images);
?>

Both the files are in root folder , and now i am getting blank page :(

Any help would be great

Thanks

  • 写回答

4条回答 默认 最新

  • dongzong8110 2013-07-17 14:29
    关注

    You can easily obtain what you want with an XPath query:

    $url = 'http://examepl.com/image.php';
    
    $doc = new DOMDocument();
    @$doc->loadHTMLFile($url);
    $xpath = new DOMXPath($doc);
    $srcs = $xpath->query("//div[@class='container']//img/attribute::src");
    foreach ($srcs as $src) {
        echo '<br/>' . $src->value; 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi