preg_match_all未从php中的文件中获取图像src
我有一个页面image.php 图像保存在容器中的位置如下:-注意:容器div之外还有其他图像。。我只想从集装箱司的图像preg_match_all未从php中的文件中获取图像src,php,preg-match-all,Php,Preg Match All,我有一个页面image.php 图像保存在容器中的位置如下:-注意:容器div之外还有其他图像。。我只想从集装箱司的图像 <!DOCTYPE html> <head> <title>Image Holder</title> </head> <body> <header> <a href="#"><img src="h
<!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>
图像夹
某个职位
某个职位
某个职位
版权及副本;2013
我正试图从带有preg_match_all的image.php文件中仅获取图像,但它返回布尔值(false):(
我的php代码:-
<?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);
?>
这两个文件都在根文件夹中,现在我得到一个空白页:(
任何帮助都会很好
谢谢preg_match_all(“/”,$match,$images);
取代
preg_match_all("/<img src=\"(.*?)\"/", $match, $images); // stripped ">" char
preg\u match\u all(“/”字符
<>代码> < p>您最好不要使用正则表达式。为此,PHP提供了良好的DOM API。请考虑如下代码:
$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);
现场演示:我想这对你会有用的。请尝试下面的链接来测试你的正则表达式
preg_match_all("/<div class=\"container\">(.*?)<\/div>/", $file, $match);
preg_match_all("/<img .*?(?=src)src=\"([^\"]+)\"/", $match[1][0], $images);
preg_match_all(“/(.*?/”,$file,$match);
预匹配全部(“/
您可以通过XPath查询轻松获得所需内容:
$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;
}
$url='1!'http://examepl.com/image.php';
$doc=新的DOMDocument();
@$doc->loadHTMLFile($url);
$xpath=新的DOMXPath($doc);
$srcs=$xpath->query(“//div[@class='container']//img/attribute::src”);
foreach($src作为$src){
回显“
”。$src->值;
}
不获取图像,它说array[2]0=>array[0]=>{}array[1]=>{}:(你能发布image.php的内容吗?如果有一个显示图像的循环,它将无法工作,因为文件\u get\u contents将获取php代码,而不是浏览器的输出;)它返回空白页面:(我有2个域,我可能必须在另一个域中使用它来获取图像:(您可以使用$file
变量像这样加载DOM:$doc->loadHTML($file);
我在中尝试了$file的代码,它返回了数组()
:(你能发布$file
变量中包含的HTML内容吗?我接受了你在问题中发布的image.php
,效果很好。我还有一个域,也需要从我的第一个域获取图像…我不想为我的两个站点编写不同的代码。@DragonSnake:没问题,用h文件名作为参数。我将@$doc->loadHTMLFile('image.php');
替换为@$doc->loadHTMLFile($file);
我的$file所在的位置,它返回了空白图像:(@dragonsake:您不需要使用file\u get\u内容,请参阅我的编辑。
preg_match_all("/<div class=\"container\">(.*?)<\/div>/", $file, $match);
preg_match_all("/<img .*?(?=src)src=\"([^\"]+)\"/", $match[1][0], $images);
$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;
}