joe92 Posted June 15, 2011 Share Posted June 15, 2011 $replaceUrlPrt5 = preg_replace("/(?<!href=\")((https?:\/\/)(www\.|ftp\.)?[\w\-]+\.[^\s]+?)(?!\")/ism", "<a class=\"nWhite\" href=\"\\2\" target=\"_blank\">\\2</a>", $replaceUrlPrt4); Prts 1 - 4 deal with replacing url links encased in tags in various ways, they work fine. However, I would also like to offer the option to just post a link to a website, not in tags, and convert the text to a hyperlink. This line of code will be after the previous preg_replace's. Because of this I need to make sure that before the url link there is not a 'href="' and at the end is not a '"' as this would indicate a hyperlink already made. I cannot search for a space at the start and end as I previously hoped, as the post may simply be just a link with no whitespace around it. This code however, takes the contents of a previously made hyperlink and tries to convert it again which throws out hyperlinks to nowhere. It will probably be something simple but I can't see it Anyone know what to do? Thank you Joe Quote Link to comment https://forums.phpfreaks.com/topic/239417-preg_replace-random-url-link/ Share on other sites More sharing options...
joe92 Posted June 15, 2011 Author Share Posted June 15, 2011 Ha, it was something simple $replaceUrlPrt5 = preg_replace("/^(?<!href=\")((https?:\/\/)(www\.|ftp\.)?[\w\-]+\.[^\s]+?)(?!\")$/ism", "<a class=\"nWhite\" href=\"\\1\" target=\"_blank\">\\1</a>", $replaceUrlPrt4); It was a case of placing anchors around the pattern, and also linking to the right parenthesis, as the look behind doesn't count! However, I found a new question... The positioning of the anchors around pattern, will the following patterns return different results? I have tried them both and they seem to return the same result. However, is one way better than the other, or is it just a case of tit for tat? 1. /^(?<!href=\")((https?:\/\/)(www\.|ftp\.)?[\w\-]+\.[^\s]+?)(?!\")$/ism 2. /(?<!href=\")^((https?:\/\/)(www\.|ftp\.)?[\w\-]+\.[^\s]+?)$(?!\")/ism Quote Link to comment https://forums.phpfreaks.com/topic/239417-preg_replace-random-url-link/#findComment-1229974 Share on other sites More sharing options...
joe92 Posted June 15, 2011 Author Share Posted June 15, 2011 Wait, it didn't work properly. I tested using only one link, forgetting to try it in random orders. I'm tired, that's my excuse. It worked if the link was at the end of the page. The obvious solution is to remove the $ anchor at the end of the pattern. However, if the link is at the end of a paragraph this then turns everything up to the end of the <br/> into a link as it is set to read as multi-line. So it required making sure there wasn't a '<' in the character class at the end. The following pattern works... (so far, fingers crossed no errors appear) $replaceUrlPrt5 = preg_replace("/^(?<!href=\")((https?:\/\/)(www\.|ftp\.)?[\w\-]+\.[^\s\<]+)(?!\")/ism", "<a class=\"nWhite\" href=\"\\1\" target=\"_blank\">\\1</a>", $replaceUrlPrt4); Quote Link to comment https://forums.phpfreaks.com/topic/239417-preg_replace-random-url-link/#findComment-1229984 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.