0

if i stored data in DB which contains urls (for example : Go thorugh this link http://www.google.com).

when i display that data in browser, i want to display that data like " Go through this link http://www.google.com ". but that url which looks like anchor link...

if you didn't get this..open google chat...send some msg to anyone like http://google.com..if u send plain text like http://google.com,but it shows with hyper link..to that url..

i want this functionality in PHP technology...how can we implement this

thanks in advance...

user71723
  • 343
  • 3
  • 5
  • 13

3 Answers3

6

So, you want to convert the urls to links in php? See the first result, or answers to same question in stackoverflow.

Community
  • 1
  • 1
kkyy
  • 12,214
  • 3
  • 32
  • 27
2

If I understood this correctly you want to transform URLs in a text to links automatically, without going further into details a crude (very crude) regexp should do it for now:

$textWithLinks = preg_replace('#(http|ftp)s?://[^\s]+#i', '<a href="$0">$0</a>', $textWithUrls);
thr
  • 19,160
  • 23
  • 93
  • 130
  • for reference: http://stackoverflow.com/questions/206059/php-validation-regex-for-url – Eineki Mar 04 '09 at 08:20
  • and also i have one doubt.. your code is not woring with the www.google.com – user71723 Mar 04 '09 at 09:55
  • because it requires the URLs to start with http, https, ftp or ftps, this can be circumvented ofc. but needs a more advanced script, I said it was *very crude* – thr Mar 06 '09 at 10:36
0
function add_href ($text) {
    return preg_replace('/((www\.|(http|https|ftp|news|file)+\:\/\/)[&#95;.a-z0-9-]+\.[a-z0-9\/&#95;:@=.+?,##%&~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])/', '<a href="$0">$0</a>', $text);
}

Expression taken from http://rickyrosario.com/blog/converting-a-url-into-a-link-in-csharp-using-regular-expressions/