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
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
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.
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);
?>