unrelenting Posted May 10, 2009 Share Posted May 10, 2009 I am trying to modify my message board posts in a way that it will display any <img> images that are located in my smileys folder but only display a <a href> link if they are located anywhere else. So if someone makes a post using a smiley located in http://mysite/forum/smileys folder then it makes no change at all because it will already be in the <img> tags. But if it is a hotlink to an image on another site or even a hotlink to an image in a different folder on my own site I'd like to change it from: <img src="http://differentsite.com/image.jpg" alt="" border="0" /> to: <a href="http://differentsite.com/image.jpg" target="_blank">http://differentsite.com/image.jpg</a> The entire post is stored in the $message variable. How would I code this to search the variable for instances of <img> tag links and ignore it if it is in the http://mysite/forum/smileys folder but change it to an <a href> link if it points to any other folder or website? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/157628-need-some-help-with-message-board-posts/ Share on other sites More sharing options...
chronister Posted May 10, 2009 Share Posted May 10, 2009 Start looking into regular expression. You are wanting to search for a pattern and if it matches do something with it based on what it is. Regular expressions are exactly what this is for. Good Luck. Quote Link to comment https://forums.phpfreaks.com/topic/157628-need-some-help-with-message-board-posts/#findComment-831242 Share on other sites More sharing options...
unrelenting Posted May 11, 2009 Author Share Posted May 11, 2009 Start looking into regular expression. You are wanting to search for a pattern and if it matches do something with it based on what it is. Regular expressions are exactly what this is for. Good Luck. I figured that is what I would need but was hoping for a different approach. Regex is the most complicated thing I have seen in PHP. Quote Link to comment https://forums.phpfreaks.com/topic/157628-need-some-help-with-message-board-posts/#findComment-831263 Share on other sites More sharing options...
chronister Posted May 11, 2009 Share Posted May 11, 2009 Yes is it a very complicated process, and I still don't understand it worth nothing. But what your wanting to do is a very complicated process. Your trying to find a particular string that could change, and then do something based on what you find. Regular expressions are the only way to go that I know of. Nate Quote Link to comment https://forums.phpfreaks.com/topic/157628-need-some-help-with-message-board-posts/#findComment-831279 Share on other sites More sharing options...
Philip Posted May 11, 2009 Share Posted May 11, 2009 Regex is the most complicated thing I have seen in PHP. It's really not that bad and is quite easy after some practice. I can move this to the Regex board if you'd like. I know they are always hungry for a new challenge there. Quote Link to comment https://forums.phpfreaks.com/topic/157628-need-some-help-with-message-board-posts/#findComment-831280 Share on other sites More sharing options...
unrelenting Posted May 11, 2009 Author Share Posted May 11, 2009 Regex is the most complicated thing I have seen in PHP. It's really not that bad and is quite easy after some practice. I can move this to the Regex board if you'd like. I know they are always hungry for a new challenge there. Please do. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/157628-need-some-help-with-message-board-posts/#findComment-831281 Share on other sites More sharing options...
nrg_alpha Posted May 11, 2009 Share Posted May 11, 2009 So if I understand you correctly, you want to examine all <img> tags in a post, and if any of their src attributes don't hold a specific value (like in your example, 'http://mysite/forum/smileys', you want to replace those image tags with anchor tags which contain those img src values? So something like this? $message = 'This image: <img src="http://differentsite.com/image.jpg" alt="" border="0" /> or this image: <img src="http://mysite/forum/smileys" alt="" /> or <img src="http://mysite/some/folder/smileys" alt="" />'; if(preg_match_all('#<img.+?src=([\'"])([^\'"]+)\1.*?/>#i', $message, $matches)){ $count = count($matches[0]); for ($a = 0 ; $a < $count ; $a++) { if(strpos($matches[2][$a], 'http://mysite/forum/smileys') === false){ // change this path to your site path $message = str_replace($matches[0][$a], '<a href="' . $matches[2][$a] . '" target="_blank">'. $matches[2][$a] . '</a>', $message); } } } echo $message; Output (via richt-click 'view source'): This image: <a href="http://differentsite.com/image.jpg" target="_blank">http://differentsite.com/image.jpg</a> or this image: <img src="http://mysite/forum/smileys" alt="" /> or <a href="http://mysite/some/folder/smileys" target="_blank">http://mysite/some/folder/smileys</a> Just a note on target="_blank". In general, target is frowned up, as this removes control from the user (accessibility issues). Additionally, according to this w3c page, if you are using a strict doc type, don't even bother with target, as it is deleted: In HTML 4.01 Transitional and XHTML 1.0 Transitional, the target attribute can be used to open a new window, instead of automatic pop-ups. (The target attribute is deleted from HTML 4.01 Strict and XHTML 1.0 Strict.) Quote Link to comment https://forums.phpfreaks.com/topic/157628-need-some-help-with-message-board-posts/#findComment-831596 Share on other sites More sharing options...
unrelenting Posted May 11, 2009 Author Share Posted May 11, 2009 You understood my request perfectly. I will give that a whirl this evening when I get home from work. I appreciate your time. I am using the same forum software that phpfreaks uses (SMF) and they have that _blank attribute built into it already so that is really out of my control. Quote Link to comment https://forums.phpfreaks.com/topic/157628-need-some-help-with-message-board-posts/#findComment-831876 Share on other sites More sharing options...
unrelenting Posted May 11, 2009 Author Share Posted May 11, 2009 So if I understand you correctly, you want to examine all <img> tags in a post, and if any of their src attributes don't hold a specific value (like in your example, 'http://mysite/forum/smileys', you want to replace those image tags with anchor tags which contain those img src values? So something like this? $message = 'This image: <img src="http://differentsite.com/image.jpg" alt="" border="0" /> or this image: <img src="http://mysite/forum/smileys" alt="" /> or <img src="http://mysite/some/folder/smileys" alt="" />'; if(preg_match_all('#<img.+?src=([\'"])([^\'"]+)\1.*?/>#i', $message, $matches)){ $count = count($matches[0]); for ($a = 0 ; $a < $count ; $a++) { if(strpos($matches[2][$a], 'http://mysite/forum/smileys') === false){ // change this path to your site path $message = str_replace($matches[0][$a], '<a href="' . $matches[2][$a] . '" target="_blank">'. $matches[2][$a] . '</a>', $message); } } } echo $message; Output (via richt-click 'view source'): This image: <a href="http://differentsite.com/image.jpg" target="_blank">http://differentsite.com/image.jpg</a> or this image: <img src="http://mysite/forum/smileys" alt="" /> or <a href="http://mysite/some/folder/smileys" target="_blank">http://mysite/some/folder/smileys</a> Just a note on target="_blank". In general, target is frowned up, as this removes control from the user (accessibility issues). Additionally, according to this w3c page, if you are using a strict doc type, don't even bother with target, as it is deleted: In HTML 4.01 Transitional and XHTML 1.0 Transitional, the target attribute can be used to open a new window, instead of automatic pop-ups. (The target attribute is deleted from HTML 4.01 Strict and XHTML 1.0 Strict.) Wow. You are pure genius. It's akin to witchcraft how you guys can make things happen. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/157628-need-some-help-with-message-board-posts/#findComment-831991 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.