从PHP中的字符串中获取多个图像src

  $ string =“< p> dsaasdsdsdas sdhio ahsas asdsad adhaso img da sda a asd a as src  asdasd ds。< / p>< p> dsaasdsdsdas sdhio ahsas asdsad adhaso img da sda a asd a as src asdasd ds。< / p>< p> dsaasdsdsdas< img src ='http:// stie。  com / teste.jpg'> ds。< / p>< p> dsaasdsdsdas sdhio ahsas asdsad adhaso img da sda a asd< img src ='http://segunda.com/teste.jpg'> a 作为src asdasd ds。< / p>< p> dsaasdsdsdas sdhio ahsas asdsad adhaso img da sda a asd a as src asdasd ds。< / p>“; 

$ doc = new DOMDocument();

$的doc-> loadHTML($字符串);

$ xpath = new DOMXPath($ doc);

$ src = $ xpath-> evaluate(“string(// img / @ src)”);

echo $ src;
</ code> </ pre>

I 有这个代码试图从PHP中的字符串中提取所有img源。
但是我的代码有问题,如果它有多个图像,它只获得第一个图像src。 我怎样才能更改这个以给我一个字符串中的所有图像src,例如一个数组?</ p>
</ div>

展开原文

原文

$string = "<p>dsaasdsdsdas sdhio ahsas asdsad adhaso img da sda a asd a as src asdasd ds.</p>   <p>dsaasdsdsdas sdhio ahsas asdsad adhaso img da sda a asd a as src asdasd ds.</p>  <p>dsaasdsdsdas <img src='http://stie.com/teste.jpg'> ds.</p>   <p>dsaasdsdsdas sdhio ahsas asdsad adhaso img da sda a asd <img src='http://segunda.com/teste.jpg'> a as src asdasd ds.</p>     <p>dsaasdsdsdas sdhio ahsas asdsad adhaso img da sda a asd a as src asdasd ds.</p>";

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

echo $src;

I have this code to try to extract all img sources from a string in PHP. But my code have an issue, if it has more than one image, it only get the first image src. How can I change this to give me all image src in a string, to an array for example?

2个回答



  preg_match('/&lt; * img [^&gt;] * src * = * [“\']?(  [^“\'] *)/ i',$ yourDomString,$ matches); 
print_r($ matches); //返回img标签的所有SRC结果。
</ code> </ pre>

了解更多关于 preg_match </ p>
</ div>

展开原文

原文

 preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $yourDomString, $matches);
print_r($matches); //returns all SRC results of img tags.

know more about preg_match



问题不在你的PHP代码中,它是XPath 1.0(你的库使用的)的特性。 如果在一组节点上使用 string()</ code>之类的函数,则该函数将仅应用于第一个</ em>项,而其他所有函数将被忽略。</ p>

不是寻找 string(// img / @src)</ code>,而是在第一步中选择所有 img </ code>元素,然后才检索它们的 getAttribute()> src </ code>属性值。</ p>

PHP </ strong> </ p>

 &lt;?php 

$ string =“&lt; p&gt; dsaasdsdsdas sdhio ahsas asdsad adhaso img da sda a asd a as src asdasd ds。&lt; / p&gt;&lt; p&gt; dsaasdsdsdas sdhio ahsas asdsad adhaso img da sda a asd a as src asdasd ds。&lt; / p&gt;&lt; p&gt; dsaasdsdsdas&lt; img src ='http://stie.com/teste.jpg'> ds。&lt; / p&gt;&lt; p&gt; dsaasdsdsdas sdhio ahsas asdsad adhaso img da sda a asd&lt; img src ='http://segunda.com /teste.jpg'> a as src asdasd ds。&lt; / p&gt;&lt; p&gt; dsaasdsdsdas sdhio ahsas asdsad adhaso img da sda a asd a as src asdasd ds。&lt; / p&gt;“;

$ doc = new DOMDocument();

$的doc-&GT; loadHTML($字符串);

$ xpath = new DOMXPath($ doc);

$ images = $ xpath-&gt; evaluate(“// img”);

foreach($ images as $ image){
$ src = $ image-&gt; getAttribute('src');
echo $ src;
echo“
”;
}

?&gt;
</ code> </ pre>

输出</ strong> </ p >

 <代码> http://stie.com/teste.jpg
http://segunda.com/teste.jpg
</代码> </ PRE>

请注意:正如另一个答案所示,解析带有正则表达式的HTML字符串在大多数情况下不是一个好主意。</ p>
</ div>

展开原文

原文

The problem is not in your PHP code, it is a peculiarity of XPath 1.0 (which your library uses). If you use a function like string() on a set of nodes, the function will only be applied to the first item and all others will be ignored.

Instead of looking for string(//img/@src), select all img elements in a first step and only then retrieve their src attribute values with getAttribute().

PHP

<?php

$string = "<p>dsaasdsdsdas sdhio ahsas asdsad adhaso img da sda a asd a as src asdasd ds.</p>   <p>dsaasdsdsdas sdhio ahsas asdsad adhaso img da sda a asd a as src asdasd ds.</p>  <p>dsaasdsdsdas <img src='http://stie.com/teste.jpg'> ds.</p>   <p>dsaasdsdsdas sdhio ahsas asdsad adhaso img da sda a asd <img src='http://segunda.com/teste.jpg'> a as src asdasd ds.</p>     <p>dsaasdsdsdas sdhio ahsas asdsad adhaso img da sda a asd a as src asdasd ds.</p>";

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

foreach ($images as $image) {
    $src = $image->getAttribute('src');
    echo $src;
    echo "
";
}

?>

Output

http://stie.com/teste.jpg
http://segunda.com/teste.jpg

Please note: parsing an HTML string with regular expressions, as the other answer suggests, is not a good idea in most cases.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐