Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用preg_replace[PHP]获取img标签详细信息_Php_Html_Regex - Fatal编程技术网

使用preg_replace[PHP]获取img标签详细信息

使用preg_replace[PHP]获取img标签详细信息,php,html,regex,Php,Html,Regex,这是我的内容: <p><img src="http://localhost/contents/uploads/2017/11/1.jpg" width="215" height="1515"></p> 这是我的PHP代码: function convert_the_content($content){ $content = preg_replace('/<p><img.+src=[\'"]([^\'"]+)[\'"].*>

这是我的内容:

<p><img src="http://localhost/contents/uploads/2017/11/1.jpg" width="215" height="1515"></p>

这是我的PHP代码:

function convert_the_content($content){
    $content = preg_replace('/<p><img.+src=[\'"]([^\'"]+)[\'"].*>/i', "<p class=\"uploaded-img\"><img class=\"lazy-load\" data-src=\"$1\" /></p>", $content);
    return $content;
}
函数转换内容($content){
$content=preg\u replace('//i',“

”,$content); 返回$content; }
我使用我的代码为
标记和
添加一个类到
数据src=”“

问题是,我的代码已从
标记中删除了宽度和高度属性,因此我的问题是如何更改代码以使其正常工作,并使用它获取此详细信息


注意:我的内容可能有许多
标记。

如果您只有这个非常精确的HTML片段,只需执行以下操作就可以更简单

$html = <<< HTML
<p><img src="http://localhost/contents/uploads/2017/11/1.jpg" width="215" height="1515"></p>
HTML;

$html = str_replace('<p>', '<p class="foo">', $html);
$html = str_replace(' src=', ' data-src=', $html);
echo $html;
$html=loadHTML($html);
libxml\u使用\u内部错误(false);
$xpath=newdomxpath($dom);
foreach($xpath->evaluate('//p[img]')作为$paragraphWithImage){
$paragraphWithImage->setAttribute('class','foo');
foreach($paragraphWithImage->getElementsByTagName('img')作为$image){
$image->setAttribute('class',trim('bar'.$image->getAttribute('class'));
$image->setAttribute('data-src',$image->getAttribute('src');
$image->removeAttribute('src');
}
};
echo$dom->saveHTML($dom->documentElement);
输出:

<html><body>
    <p class="foo"><img width="215" height="1515" class="bar" data-src="http://localhost/contents/uploads/2017/11/1.jpg"></p>
    <p class="foo"><img width="215" height="1515" class="bar" data-src="http://localhost/contents/uploads/2017/11/1.png"></p>
      <p class="foo"><img class="bar blah" height="1515" width="215" data-src="http://localhost/contents/uploads/2017/11/1.png"></p>
  </body></html>


不,它不是一个精确的HTML,因为我想使用preg\u replace,所以不可能使用preg\u replace完成我需要的操作?为任意HTML编写可靠的模式是可能的,但非常困难。dom解析器是simpler@Gordon不幸的是,我试图说服OP使用DomDocument回答一些问题,但没有结果:(@Gordon谢谢,但我认为有点不对劲代码正在工作,为标记添加了一个类,是的,但它没有为图像更改任何内容它没有为标记添加一个类,也没有将src=更改为数据src=@sadadadadadadad抱歉。修复了。
<?php
$html = <<< HTML
<html>
  <body>
    <p><img src="http://localhost/contents/uploads/2017/11/1.jpg" width="215" height="1515"></p>
    <p><img width="215" height="1515" src="http://localhost/contents/uploads/2017/11/1.png"></p>
      <p   ><img
        class="blah"
            height="1515" 
            width="215"
       src="http://localhost/contents/uploads/2017/11/1.png"

       >
    </p>
  </body>
</html>
HTML;

$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_use_internal_errors(false);

$xpath = new DOMXPath($dom);
foreach ($xpath->evaluate('//p[img]') as $paragraphWithImage) {
    $paragraphWithImage->setAttribute('class', 'foo');
    foreach ($paragraphWithImage->getElementsByTagName('img') as $image) {
        $image->setAttribute('class', trim('bar ' . $image->getAttribute('class')));
        $image->setAttribute('data-src', $image->getAttribute('src'));
        $image->removeAttribute('src');
    }
};

echo $dom->saveHTML($dom->documentElement);
<html><body>
    <p class="foo"><img width="215" height="1515" class="bar" data-src="http://localhost/contents/uploads/2017/11/1.jpg"></p>
    <p class="foo"><img width="215" height="1515" class="bar" data-src="http://localhost/contents/uploads/2017/11/1.png"></p>
      <p class="foo"><img class="bar blah" height="1515" width="215" data-src="http://localhost/contents/uploads/2017/11/1.png"></p>
  </body></html>