Harry
Harry

Reputation: 21

Remove IMG tag from String for a specific image with PHP

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?

Upvotes: 1

Views: 3925

Answers (2)

DLouren&#231;o
DLouren&#231;o

Reputation: 11

if you meant to extract attributes, try

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

Upvotes: 0

Yoshi
Yoshi

Reputation: 54659

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?

Upvotes: 1

Related Questions