Hi there- I'm looking for a way to find a URL within a string and replace it with an a href tag including said URL. For instance, I've gotten the below to work, but it only accounts for "http" string, not ftp or https:
Code: Select all
$pattern = '((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)';
$string = 'This text needs to stay while the following URL needs to be wrapped in an a href tag: http://www.ohyeah.com';
$replacement = '<a href="">'.strstr($string, "http").'</a>';
echo preg_replace($pattern, $replacement, $string);
Is there a preg function that returns the pattern string only? I would use it in place of the strstr function within the $replacement var. Thanks any help is appreciated!