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. Link to comment https://forums.phpfreaks.com/topic/48573-need-some-help-with-regular-expressions/ 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? Link to comment https://forums.phpfreaks.com/topic/48573-need-some-help-with-regular-expressions/#findComment-237835 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> Link to comment https://forums.phpfreaks.com/topic/48573-need-some-help-with-regular-expressions/#findComment-237838 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... Link to comment https://forums.phpfreaks.com/topic/48573-need-some-help-with-regular-expressions/#findComment-237848 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. Link to comment https://forums.phpfreaks.com/topic/48573-need-some-help-with-regular-expressions/#findComment-237873 Share on other sites More sharing options...
benjaminbeazy Posted April 25, 2007 Share Posted April 25, 2007 is it always smilie.gif???? Link to comment https://forums.phpfreaks.com/topic/48573-need-some-help-with-regular-expressions/#findComment-237887 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. Link to comment https://forums.phpfreaks.com/topic/48573-need-some-help-with-regular-expressions/#findComment-237894 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 Link to comment https://forums.phpfreaks.com/topic/48573-need-some-help-with-regular-expressions/#findComment-237913 Share on other sites More sharing options...
komakino Posted April 25, 2007 Author Share Posted April 25, 2007 thanks anyway. Link to comment https://forums.phpfreaks.com/topic/48573-need-some-help-with-regular-expressions/#findComment-237916 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> Link to comment https://forums.phpfreaks.com/topic/48573-need-some-help-with-regular-expressions/#findComment-238155 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.