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 © 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
我有页面image.php p>
其中图像保存在容器中 下面: - 注意:容器div外还有其他图像..我只想要容器div中的图像。 p>
&lt;!DOCTYPE html&gt;
&lt; head&gt; \ n&lt; title&gt;图像持有人&lt; / title&gt;
&lt; / head&gt;
&lt; body&gt;
&lt; header&gt;
&lt; a href =“#”&gt;&lt; img src =“http: //examepl.com/logo.png"></a>
&lt; div id =“side”&gt;
&lt; div id =“facebook”&gt;&lt; img src =“http:/ /examepl.com/fb.png"></div>
&lt; div id =“twiiter”&gt;&lt; img src =“http://examepl.com/t.png”&gt;&lt; / div&gt;
&lt; div id =“gplus”&gt;&lt; img src =“http://examepl.com/gp.png”&gt;&lt; / div&gt;
&lt; / div&gt;
&lt; ; /报头&GT;
&lt; div class =“container”&gt;
&lt; p&gt; SOme Post&lt; / p&gt;
&lt; img src =“http://examepl.com/some.png”title =“some image” /&gt;
&lt; p&gt; SOme发布&lt; / p&gt;
&lt; img src =“http://examepl.com/some.png”title =“some image”/&gt;
&lt; p&gt; SOme Post&lt; / p&gt;
&lt; img src =“http://examepl.com/some.png”title =“some image”/&gt;
&lt; / div&gt;
&lt; 页脚&gt;
&lt; div id =“foot”&gt;
copyright and amp; copy; 2013
&lt; / div&gt;
&lt; / footer&gt;
&lt; / body&gt;
&lt; / html&gt;
code> pre>
我正在尝试 仅使用preg_match_all从我的image.php文件中获取图像,但它返回boolean(false):( p>
我的php代码: - p>
&lt;?php
$ file = file_get_contents(“image.php”);
preg_match_all(“/&lt; div class = \”container \“&gt;(。*?)&lt; / div&gt; /”,$ file,$ match);
preg_match_all(“/&lt; img src = \”(。*?)\“&gt; /”,$ match,$ images);
var_dump($ images);
?&gt; ;
code> pre>
这两个文件都在根文件夹中,现在我得到空白页面:( p>
任何帮助都是 很好 p>
谢谢 p>
div>
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;
}
preg_match_all("/<img src=\"(.*?)\">/", $match, $images);
replace with
preg_match_all("/<img src=\"(.*?)\"/", $match, $images); // stripped ">" char
You better not use regex for this purpose. PHP provides nice DOM api for this purpose. Consider code like below:
$html = <<< EOF
<div class="container">
<p>SOme Post</p>
<img src="http://examepl.com/some1.png" title="some image" />
<p>SOme Post</p>
<img src="http://examepl.com/some2.png" title="some image" />
<p>SOme Post</p>
<img src="http://examepl.com/some3.png" title="some image" />
</div>
EOF;
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html); // loads your html
$xpath = new DOMXPath($doc);
$nodelist = $xpath->query("//div[@class='container']/img");
$img = array();
for($i=0; $i < $nodelist->length; $i++) {
$node = $nodelist->item($i);
$img[] = $node->getAttribute('src');
}
print_r($img);
OUTPUT:
Array
(
[0] => http://examepl.com/some1.png
[1] => http://examepl.com/some2.png
[2] => http://examepl.com/some3.png
)
Live Demo: http://ideone.com/iBhVMF
I think this will work for you try the link below to test your regex
preg_match_all("/<div class=\"container\">(.*?)<\/div>/", $file, $match);
preg_match_all("/<img .*?(?=src)src=\"([^\"]+)\"/", $match[1][0], $images);