komakino Posted April 25, 2007 Share Posted April 25, 2007 Hi, i really suck when it comes to regular expressions. I would like to find en HTML element with a certain css class in a string, and then add a link tag to it. I want to find: <img src="..." class="myclass" /> And make it into: <a href="..."><img src="..." class="myclass" /></a> Can someone help? I dont know where to start. Quote Link to comment Share on other sites More sharing options...
benjaminbeazy Posted April 25, 2007 Share Posted April 25, 2007 does the img src "..." represent anything, i.e.e does this value change? Quote Link to comment Share on other sites More sharing options...
komakino Posted April 25, 2007 Author Share Posted April 25, 2007 no sorry... i'll make it clearer... I want to find(by the class name): <img src="smilie.gif" class="myclass" /> And make it into: <a href="http://www.google.se"><img src="smilie.gif" class="myclass" /></a> Quote Link to comment Share on other sites More sharing options...
benjaminbeazy Posted April 25, 2007 Share Posted April 25, 2007 i suck at regex, what i'm thinking is use split() to split the string or page or whatever by "myclass" to where you can cut it off just right, then use another split or strstr to capture the image tag. then its home free... Quote Link to comment Share on other sites More sharing options...
komakino Posted April 25, 2007 Author Share Posted April 25, 2007 Another correction. What i really need is: <img src="smilie.gif" class="myclass" /> to become: <a href="smilie.gif"><img src="smilie.gif" class="myclass" /></a> I cant do this on my own. Please help. Quote Link to comment Share on other sites More sharing options...
benjaminbeazy Posted April 25, 2007 Share Posted April 25, 2007 is it always smilie.gif???? Quote Link to comment Share on other sites More sharing options...
komakino Posted April 25, 2007 Author Share Posted April 25, 2007 Nono, it can be anything, but i alsa need the src from the img in the href. Quote Link to comment Share on other sites More sharing options...
benjaminbeazy Posted April 25, 2007 Share Posted April 25, 2007 i'm too burned out to mess with regex, sorry man. try google or search the forums for similar stuff like what i suggested Quote Link to comment Share on other sites More sharing options...
komakino Posted April 25, 2007 Author Share Posted April 25, 2007 thanks anyway. Quote Link to comment Share on other sites More sharing options...
effigy Posted April 25, 2007 Share Posted April 25, 2007 <pre> <?php $string = '<img src="..." class="myclass" />'; echo htmlspecialchars(preg_replace('/(<img.+?class="myclass"[^>]*>)/', '<a href="url">\1</a>', $string)); ?> </pre> Quote Link to comment 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.