Oberti Posted October 29, 2015 Share Posted October 29, 2015 I'm using preg_replace for adding a text below images (my regex is here) /<img.+?>/i It's fine when the img is alone, it gets converted from <img src="image.jpg" border="0" alt="" /> to: <img src="image.jpg" border="0" alt="" /> <br /> Text added here However sometimes the image is contained on a link <a href="http://www.google.com/"><img src="image.jpg" border="0" alt="" /></a> So the text in this case is placed inside the <a href, which is not what i want <a href="http://www.google.com/"><img src="image.jpg" border="0" alt="" /> <br /> Text added here</a> I need to achieve something like this with regexes, but i'm not sure how can i do this. if(<img src="image.jpg" border="0" alt="" />){echo '<br /> Text here';}else if(<a href="http://www.google.com/"><img src="image.jpg" border="0" alt="" /></a>) {echo '<br /> Text here';}Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/298900-regex-conditionals/ Share on other sites More sharing options...
Ch0cu3r Posted October 29, 2015 Share Posted October 29, 2015 Change your regex to be #<img[^>]+>(\s*</a>)?#i The regex will optionally match a </a> if it comes after the <img /> tag Live PHP replacement demo (click the preg_replace tab). Quote Link to comment https://forums.phpfreaks.com/topic/298900-regex-conditionals/#findComment-1524659 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.