avvllvva Posted October 21, 2009 Share Posted October 21, 2009 Hi, Here I have a plain-text string, it may contain one or more URLs inside it. I want to make all of them into hyperlinks. This Example explain more :- <?php $str = " Plain text plain text plain textplain textplain textplain text plain text plain text plain text plain text. Plain text plain text plain text plain text plain text plain text plain www.google.com/analytics text plain text plain text. Plain text plain text plain text plain text plain text plain text plain text plain text plain text plain text http://yahoo.com plain text plain text plain text plain text plain text plain text plain text plain text plain text plain text plain text. Plain text plain text plain text plain text https://www.example.com plain text plain text plain text plain text. "; ?> In the above string you can see 3 URLs, say 1. www.google.com/analytics 2. http://yahoo.com 3. https://www.example.com (I think, i've covered all possible formats of an URL in the above three.) So whenever echoing this string I want to make these URLs into hyperlinks. ie; appending anchor tag at the begining. Output like this: ---------------------------------------------------------------------------------------------- Plain text plain text plain textplain textplain textplain text plain text plain text plain text plain text. Plain text plain text plain text plain text plain text plain text plain www.google.com/analytics text plain text plain text. Plain text plain text plain text plain text plain text plain text plain text plain text plain text plain text http://yahoo.com plain text plain text plain text plain text plain text plain text plain text plain text plain text plain text plain text. Plain text plain text plain text plain text https://www.example.com plain text plain text plain text plain text. ---------------------------------------------------------------------------------------------- Can anybody help me to acheving this? Quote Link to comment https://forums.phpfreaks.com/topic/178430-solved-making-all-urls-in-a-string-into-hyperlinks/ Share on other sites More sharing options...
dreamwest Posted October 21, 2009 Share Posted October 21, 2009 $str = " Plain text plain text plain textplain textplain textplain text plain text plain text plain text plain text. Plain text plain text plain text plain text plain text plain text plain www.google.com/analytics text plain text plain text. Plain text plain text plain text plain text plain text plain text plain text plain text plain text plain text http://yahoo.com plain text plain text plain text plain text plain text plain text plain text plain text plain text plain text plain text. Plain text plain text plain text plain text https://www.example.com plain text plain text plain text plain text. "; preg_match_all('~http://(.*?).com~is', $str, $matches); foreach ($matches[1] as $link) { echo "<a href = 'http://{$link}.com'>{$link}</a><br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/178430-solved-making-all-urls-in-a-string-into-hyperlinks/#findComment-940944 Share on other sites More sharing options...
avvllvva Posted October 21, 2009 Author Share Posted October 21, 2009 Thank you for your help. It outputs only a word "yahoo" but it is hyper linked. ie; ---------------------------------------------------------------------------------------------- yahoo ---------------------------------------------------------------------------------------------- but i'm looking for this ---------------------------------------------------------------------------------------------- Plain text plain text plain textplain textplain textplain text plain text plain text plain text plain text. Plain text plain text plain text plain text plain text plain text plain www.google.com/analytics text plain text plain text. Plain text plain text plain text plain text plain text plain text plain text plain text plain text plain text http://yahoo.com plain text plain text plain text plain text plain text plain text plain text plain text plain text plain text plain text. Plain text plain text plain text plain text https://www.example.com plain text plain text plain text plain text. ---------------------------------------------------------------------------------------------- Quote Link to comment https://forums.phpfreaks.com/topic/178430-solved-making-all-urls-in-a-string-into-hyperlinks/#findComment-940959 Share on other sites More sharing options...
avvllvva Posted October 21, 2009 Author Share Posted October 21, 2009 Finally I got the solution from php.net, here is the one <?php function addLinks($string) { $string = preg_replace("/(^|[^=\"\/])\b((\w+:\/\/|www\.)[^\s<]+)". "((\W+|\b)([\s<]|$))/i", "$1<a href=\"$2\" target='_blank'>$2</a>$4", $string); return preg_replace("/href=\"www/i", "href=\"http://www", $string); } echo addLinks($str); ?> Regular expression did the job, anyway I have to study more on that. Quote Link to comment https://forums.phpfreaks.com/topic/178430-solved-making-all-urls-in-a-string-into-hyperlinks/#findComment-940994 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.