lynxus Posted November 2, 2011 Share Posted November 2, 2011 Hi Guys, I have the following code that uses ereg however im useless when it comes to regex so I dont know how to move to preg .. Essentially, It takes a string and makes a URL "clickable" ie: if it finds a link like http://www.google.com it would change http://www.google.com to a proper html href Could someone help convert this to preg so my code is more compatible with newer version of PHP.. Thanks muchly in advance. Graham $message = "Have you seen google? http://www.google.com "; $message = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]"," :link: <a title=\"Click to open in a new window\" alt=\"Click to open in a new window\" target=\"blank\" href=\"\\0\">\\0</a>", $message); Quote Link to comment https://forums.phpfreaks.com/topic/250282-ereg_replace-to-preg_replace-ereg-depricated/ Share on other sites More sharing options...
joe92 Posted November 2, 2011 Share Posted November 2, 2011 Here's a discussion with myself as I went down this path. Go to the last post for a preg replace that works on a random url link http://www.phpfreaks.com/forums/index.php?topic=336117.0 (You don't need parts 1-4 as they deal with url links enclosed in bb code) You will also need the following line after prt5; $replaceUrlPrt6 = preg_replace("/^(?<!href=\")(https?:\/\/)?((www|ftp)\.[\w\-]+\.[^\s\<]+)(?!\")/ism", "<a class=\"nWhite\" href=\"http://\\2\" target=\"_blank\">\\2</a>", $replaceUrlPrt5); This is so that you can have addresses that start with or without http:// or with or without www. but need at least one or the other Quote Link to comment https://forums.phpfreaks.com/topic/250282-ereg_replace-to-preg_replace-ereg-depricated/#findComment-1284220 Share on other sites More sharing options...
lynxus Posted November 2, 2011 Author Share Posted November 2, 2011 Thanks very much for your reply. It doesn't seem to do anything? ( it doesn't seem to replace anything and no errors in logs.. ) <html><body> <?php $message = "Why not visit www.google.com or google.com or http://www.google.com or http://google.com or www.google.co.uk or http://google.co.uk "; $message = preg_replace("/^(?<!href=\")(https?:\/\/)?((www|ftp)\.[\w\-]+\.[^\s\<]+)(?!\")/ism", "<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $message); echo $message; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/250282-ereg_replace-to-preg_replace-ereg-depricated/#findComment-1284227 Share on other sites More sharing options...
joe92 Posted November 2, 2011 Share Posted November 2, 2011 Oh wow, glad you spotted that! I thought I had it all sorted for my forum, I clearly hadn't done a thorough testing. The following almost works except for the second part runs the regex again on the third hyperlink. I'm not sure why this is and am going to have to sort this out later. Replacing just google.com does not work (and I don't think it does in many forums) but could possibly be added by searching for a mixture of characters that finishes with .com or .co.uk etc.. I'll be working on this regex tonight so I'll let you know when it is properly working. What I did so far was to remove the caret which was making it only look for links at the beginning of the block of text. <?php $message = "Why not visit www.google.com or google.com or http://www.google.com or http://google.com or www.google.co.uk or http://google.co.uk "; $message = preg_replace("/(?<!href=\")((https?:\/\/)(www\.|ftp\.)?[\w\-]+\.[^\s\<]+)(?!\")/ism", "<a href=\"\\1\" target=\"_blank\">\\1</a>", $message); $message = preg_replace("/(?<!href=\")(https?:\/\/)?((www|ftp)\.[\w\-]+\.[^\s\<]+)(?!\")/ism", "<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $message); echo $message; ?> Quote Link to comment https://forums.phpfreaks.com/topic/250282-ereg_replace-to-preg_replace-ereg-depricated/#findComment-1284285 Share on other sites More sharing options...
lynxus Posted November 2, 2011 Author Share Posted November 2, 2011 Hi, Thanks for taking a look again.. yeah google.com etc dont worry about ( as long as it starts with www. or http:// or ftp. ) is good enough for me. I only put that in the $message as a test to see what your regex did Quote Link to comment https://forums.phpfreaks.com/topic/250282-ereg_replace-to-preg_replace-ereg-depricated/#findComment-1284290 Share on other sites More sharing options...
joe92 Posted November 5, 2011 Share Posted November 5, 2011 http://www.phpfreaks.com/forums/index.php?topic=347034.0 Got it working to account for everything Quote Link to comment https://forums.phpfreaks.com/topic/250282-ereg_replace-to-preg_replace-ereg-depricated/#findComment-1285290 Share on other sites More sharing options...
lynxus Posted November 5, 2011 Author Share Posted November 5, 2011 Good times. Thanks muchly. Been trying to learn regex and my brain hurts lol.. Getting there though Quote Link to comment https://forums.phpfreaks.com/topic/250282-ereg_replace-to-preg_replace-ereg-depricated/#findComment-1285294 Share on other sites More sharing options...
lynxus Posted November 5, 2011 Author Share Posted November 5, 2011 Although, I personally didnt use message1 and message2.... The below should work anyway without the need to generate new strings.: $message = preg_replace("/(?<!href=\")((https?:\/\/)(www\.|ftp\.)?[\w\-]+\.[^\s\<]+)(?!\")/ism", "<a href=\"\\1\" target=\"_blank\">\\1</a>", $message); $message = preg_replace("/(?<!http:\/\/)(?<!https:\/\/)((www|ftp)\.[\w\-]+\.[^\s\<]+)(?!\")/ism", "<a href=\"http://\\1\" target=\"_blank\">\\1</a>", $message); Quote Link to comment https://forums.phpfreaks.com/topic/250282-ereg_replace-to-preg_replace-ereg-depricated/#findComment-1285295 Share on other sites More sharing options...
joe92 Posted November 5, 2011 Share Posted November 5, 2011 Yeah it should. I just make the two strings because I find it easier to locate where the regex is going wrong as I'm testing it. Quote Link to comment https://forums.phpfreaks.com/topic/250282-ereg_replace-to-preg_replace-ereg-depricated/#findComment-1285296 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.