I want to make it so that any occurance of an image gets wrapped with a link to the image source
How can I write a pattern, in PHP so that I can find these variations, which are scattered throughout text coming from the database:
<img src='/dir/dir2/image1.jpg' alt='blah blah blah'> <img src="/dir/dir2/image2.jpg" alt="blah blah blah" /> <img src="/dir/dir2/image3.jpg" />
In all cases, I want them to appear within an link.
preg_replace("{<img\\s*(.*?)src=('.*?'|\".*?\"|[^\\s]+)(.*?)\\s*/?>}ims", '<a href=$2><img $1src=$2 $3/></a>', $str)
handles all non-practical cases
My I recommend the PHP DOM with loadHTML()
instead of regex?
http://php.net/dom
http://php.net/domdocument.loadhtml
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With