1

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?

genesis
  • 50,477
  • 20
  • 96
  • 125
Harry
  • 21
  • 1
  • 3
  • possible duplicate of [Regex Remove Images with style tag from Html](http://stackoverflow.com/questions/2772782/regex-remove-images-with-style-tag-from-html) – Gordon Jul 15 '11 at 15:18
  • *(related)* [Best Methods to parse HTML](http://stackoverflow.com/questions/3577641/best-methods-to-parse-html/3577662#3577662) – Gordon Jul 15 '11 at 15:20

2 Answers2

1

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?

Yoshi
  • 54,081
  • 14
  • 89
  • 103
0

if you meant to extract attributes, try

$xpath = new DOMXPath(@DOMDocument::loadHTML($html));
$src = $xpath->evaluate("string(//img/@src)");
DLourenço
  • 11
  • 3