AndyPSV Posted January 12, 2012 Share Posted January 12, 2012 I've got function that works pretty fine (it replaces http:// & www with links <a href='' etc.), but the problem is that when it finds "www" phrase in the text it also replaces it with link What I want to do is to make it only replace it, when it appear www. <- "dot"; something here & "." <- dot + 1 sign at least. as an example: 'www.p.pl' would be done as link, but "www", "www.", "www.s", "www..", "www.s." wouldn't be Hope, it's clear; thanks. function rplc($x,$style='') { /* IF _SERVER[HOST], the same -> NO: target=_blank */ $x = ereg_replace('[-a-z0-9!#$%&\'*+/=?^_`{|}~]+@([.]?[a-zA-Z0-9_/-])*','<a href=\'mailto:\\0\' '.$style.'>\\0</a>',$x); $x = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a target='_blank' href='\\0'>\\0</a>",$x); $x = ereg_replace("(^| |.)(www([.]?[a-zA-Z0-9_/-])*)", "\\1<a target='_blank' href=\"http://\\2\">\\2</a>",$x); return $x; } Quote Link to comment https://forums.phpfreaks.com/topic/254905-how-to-make-preg_match-for-links-in-the-text/ Share on other sites More sharing options...
ragax Posted January 12, 2012 Share Posted January 12, 2012 Hi Andy! Run this sample code, I think it does what you want. Input: an example: www.p.pl would be done as link, but "www", "www.", "www.s", "www..", "www.s." wouldn\'t Code: <?php $pattern=',(www\.(\w+(?:\.\w+)+)),'; $replacement='<a target=\'_blank\' href="http:\1">\2</a>'; $subject='an example: www.p.pl would be done as link, but "www", "www.", "www.s", "www..", "www.s." wouldn\'t '; $s=preg_replace($pattern,$replacement,$subject); echo htmlentities($s).'<br />'; ?> Output: an example: <a target='_blank' href="http:www.p.pl">p.pl</a> would be done as link, but "www", "www.", "www.s", "www..", "www.s." wouldn't Is this what you're looking for? Quote Link to comment https://forums.phpfreaks.com/topic/254905-how-to-make-preg_match-for-links-in-the-text/#findComment-1307027 Share on other sites More sharing options...
AndyPSV Posted January 12, 2012 Author Share Posted January 12, 2012 Yes, thank you. Quote Link to comment https://forums.phpfreaks.com/topic/254905-how-to-make-preg_match-for-links-in-the-text/#findComment-1307030 Share on other sites More sharing options...
ragax Posted January 12, 2012 Share Posted January 12, 2012 Fantastic, glad it worked for you. Quote Link to comment https://forums.phpfreaks.com/topic/254905-how-to-make-preg_match-for-links-in-the-text/#findComment-1307032 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.