I need help turning something like:
<img class="coup-pic" src="http://example.com/img/pic.jg" alt="" />
into:
<a href="http://example.com/img/pic.jg"><img class="coup-pic" src="http://example.com/img/pic.jg" alt="" /></a>
I need to be able to dynamically search for a string like the one above using PHP and wrap it in an a tag with the src attribute used as the a tag. I think I'll need to use preg_replace, but I do not know the right regex to use.
Thanks for the help!
我目前不在哪里可以测试这个,但尝试一下:
$html = preg_replace('/(<img [^>]*src="([^"]*)"[^>]*>)/i', '<a href="$2">$1</a>', $html);
这似乎有效:
preg_replace('/\\<img class="([a-zA-Z0-9\\-]*)" src="([A-Za-z0-9\\-\\.\\:\\/]*)" alt=""\\/\\>/','<a href="\\\\2"><img class="\\\\1" src="\\\\2" alt=""/></a>',$string);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.