PHP Regex remove IMG attributes and change SRC

Asked

Viewed 621 times

3

I have the following string with the html:

teste = "<img src="image/teste.jpg" alt="" width="32" height="32">
<p>teste</p>
<img src="image/teste2.jpg" alt="" width="132" height="132">";

I would like a regular expression that removes the attributes, leaving only the src and adding to the content of src a domain of all the images in the string, thus:

teste = "<img src="http://meudominio.com/image/teste.jpg">
<p>teste</p>
<img src="http://meudominio.com/image/teste2.jpg">";

Remembering that this string will not always come in that order, someone has some idea of how to do?

1 answer

1


After doing some tests I managed to make a solution, follow the online link = http://www.regexr.com/3bkaa

The code in php was like this:

$html = preg_replace('#<img.+?src=[\'"]([^\'"]+)[\'"].*>#i', '<img src="http://meudominio.com/$1">', $html);
  • 1

    interesting. intelligent solution

Browser other questions tagged

You are not signed in. Login or sign up in order to post.