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条)

报告相同问题?

悬赏问题

  • ¥30 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页