0

Find a Link in PHP String and convert it into a Hyperlink so it becomes Clickable and opens in New Tab.

PHP Code / String:

<?php echo $post_details['description']; ?>

Test Link:

http://www.test.com
Phil
  • 157,677
  • 23
  • 242
  • 245
Hassan Sardar
  • 4,413
  • 17
  • 56
  • 92
  • 2
    Does this help? http://stackoverflow.com/questions/5024126/what-is-the-php-regex-to-convert-text-containing-a-url-into-a-hyperlink?rq=1 – FMCorz Oct 16 '13 at 04:28
  • 2
    possible duplicate of [Replace URLs in text with HTML links](http://stackoverflow.com/questions/1188129/replace-urls-in-text-with-html-links) – Phil Oct 16 '13 at 04:29
  • 2
    You may refer to http://stackoverflow.com/questions/247479/jquery-text-to-link-script – Samuel Oct 16 '13 at 04:30
  • Thats great it helped me. Plus i want to ask, what if i also want to show a thumbail from that link? – Hassan Sardar Oct 16 '13 at 04:53

2 Answers2

0

use this library. it automatically detect link from the string and make it clickable. you don't need to create a link just add a string having a link. this library will detect it automatically.

http://soapbox.github.io/jQuery-linkify/

Muhammad Tahir
  • 2,351
  • 29
  • 25
-2

Use this example to solve your problem

<?php

function makeClickableLink($text) {
    $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\\1">\\1</a>', $text);
    $text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '\\1<a href="http://\\2">\\2</a>', $text);
    $text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})', '<a href="mailto:\\1">\\1</a>', $text);
    return $text;
}

// Usage 

// Email address example
$text = "you@example.com";
echo makeClickableLink($text);

// URL example
$text = "http://www.example.com";
echo makeClickableLink($text);  

// FTP URL example
$text = "ftp://ftp.example.com";
echo makeClickableLink($text); 

?>