Hello,
I am trying to manipulate a HTML content stored in a php variable.
The logic is as follows: The variable should be checked if it contains an <img ... /> tag and if a tag is found it should be wraped in an <a> tag and some parametres of the a tag should be read from the img tag.
The code in the end enables the fancybox plugin to all images, this can be done even via JS, but in my case I would like to have the content ready as it would be served to the visitor.
Example:
The initial content.
$content = 'This content has two images
<img src="/images/img1.jpg" alt="Image 1" />
<img src="/images/img2.jpg" alt="Image 2" />';
The final content
$content = 'This content has two images
<a href="/images/img1.jpg" data-fancybox="image-popup" data-caption="Image 1">
<img src="/images/img1.jpg" alt="Image 1" />
</a>
<a href="/images/img2.jpg" data-fancybox="image-popup" data-caption="Image 2">
<img src="/images/img2.jpg" alt="Image 2" />
</a>';
Thank you!