Friday, 6 September 2013

preg_replace - turn into

preg_replace - turn into

I'm struggling to turn an image tag into a link and copy parameters within
the tag i.e. <img src="/images/image1.jpg" alt="The name of image">
into <a href="/images/image1.jpg" target="_self" rel="zoom" title="The
name of image"><img src="/images/image1.jpg" alt="The name of image"></a>
My problem is not just duplicating the src and alt data but account for
missing and extra tags i.e. <img src="/images/image1.jpg" >
into <a href="/images/image1.jpg" target="_self" rel="zoom" title=""><img
src="/images/image1.jpg" ></a>
and <img style="padding:5px;" src="/images/image1.jpg" alt="The name of
the image">
into <a href="/images/image1.jpg" target="_self" rel="zoom" title="The
name of the image"><img style="padding:5px;" src="/images/image1.jpg"
alt="The name of the image"></a>
This needs to be done for all instances of the img tag throughout a string.
Not meaning to sound like a challenge but can anyone propose a possible
solution, I'm sure this can be done with preg_replace but I just can't
work it through?
Thank you

No comments:

Post a Comment