使用PHP从特定于图像的String中删除IMG标记

使用PHP从特定于图像的String中删除IMG标记

问题描述:

I have a block of text that has a particular image that I want to strip out. The problem is that the tag can be with different styles

for e.g

<img src="myimage.png" alt="" class=""/>  

or

<img alt="" class="" src="myimage.png"/>

or

<img class="" alt ="" src="myimage.png"/>

Now how can I remove that particular image tag from my string using PHP?

我有一个文本块,其中包含我想要删除的特定图像。 问题是标签可以有不同的样式 p>

例如 p>

 &lt; img src =“myimage.png”alt =  “”class =“”/&gt;  
  code>  pre> 
 
 

或 p>

 &lt; img alt =“”class =“”src =“myimage.png”  /&gt; 
  code>  pre> 
 
 

或 p>

 &lt; img class =“”alt =“”src =“myimage  .png“/&gt; 
  code>  pre> 
 
 

现在如何使用PHP从我的字符串中删除该特定图像标记? p> div>

Something like:

$str = 'Lorem <img alt="" class="" src="myimage.png"/> ipsum <img class="" alt="" src="myimage.png"/> dolor <img src="myimage.png"/> sit...';
echo preg_replace('!<img.*?src="myimage.png".*?/>!i', '', $str);
// output: "Lorem  ipsum  dolor  sit..."

maybe?

if you meant to extract attributes, try

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