I would like to add links around image tags with preg_replace().
Before:
<img href="" src="" alt="" />
After:
<a href="" ..><img href="" src="" alt="" /></a>
I would greatly appreciate any help. Thank you very much.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Would this help?
Also,
preg_replace_callbackgives you great power in terms of dynamically determining the actual contents of the<a>tag.EDIT: To safeguard against the flaw @Amber pointed out, this pattern should help:
YMMV with that, depending on the uniformity of your
<img>tags. Is alt always present and the last attribute, with single spaces around etc.EDIT: Re: copying img’s src to a’s href:
And again .. this is expecting uniformity from your original
imgtags. If they are created by you, you may be good as is. If not, you’ll want to safeguard against missing attributes, varying order, double vs single quotes etc.