Flipperbw Posted September 16, 2010 Share Posted September 16, 2010 The Issue My users can post comments. I want to turn the hidden links within those comments into clickable URLs. I have the following function to do this: if (!function_exists('create_link')) { function create_link($url) { $pattern = "#((http|https|ftp)://(\S*?\.\S*?))(\s|\;|\)|\]|\[|\{|\}|,|\"|'|:|\<|$|\.\s)#ie"; $link_title = character_limiter(preg_replace($pattern, "$3", $url), 50); $link = preg_replace($pattern, "'<a href=\"$1\" target=\"_blank\">{LINK_TITLE}</a>$4'", $url); if ($link == $url) { $url = 'http://' . $url; $link = preg_replace($pattern, "'<a href=\"$1\" target=\"_blank\">{LINK_TITLE}</a>$4'", $url); } if ($link != $url) { $result = str_replace('{LINK_TITLE}', $link_title, $link); } else { $result = FALSE; } return $result; } } I know that the regex is pretty bad. It kind of works, but makes every single thing with a period into a link, and messes up sometimes with target= portion where it will actually display that. I've tried a bunch of other regex's but every single one gives me problems, typically like "php unknown modifier ]" or ")" or something. here's how I call this function when users hit submit in the comment field: $words = preg_split("/[\s,]+/", $comment); foreach ($words as $word) { if ($link = create_link($word)) { $comment = str_replace($word, $link, $comment); I just want to convert the right links into links. is something like: ^(((ht|f)tp(s?))\://)?(www.|[a-zA-Z].)[a-zA-Z0-9\-\.]+\.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\;\?\'\\\+&%\$#\=~_\-]+))*$ any better? I don't know, nothing seems to work. Quote Link to comment https://forums.phpfreaks.com/topic/213597-preg_replace-problems-trying-to-convert-parts-of-submitted-text-to-urls/ Share on other sites More sharing options...
Adam Posted September 19, 2010 Share Posted September 19, 2010 Have you tried Google..? http://www.google.co.uk/search?q=php+regex+turn+urls+into+links Quote Link to comment https://forums.phpfreaks.com/topic/213597-preg_replace-problems-trying-to-convert-parts-of-submitted-text-to-urls/#findComment-1112905 Share on other sites More sharing options...
Flipperbw Posted September 20, 2010 Author Share Posted September 20, 2010 Problem was solved. Not easily identifiable with google, the issue was in the way I had been splitting up words for a check. Sending the whole comment to an array solved the problem. Quote Link to comment https://forums.phpfreaks.com/topic/213597-preg_replace-problems-trying-to-convert-parts-of-submitted-text-to-urls/#findComment-1113211 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.