I have a bunch of strings concatenated together into one that contain text and links. I want to find URLs in the string and want to put href to each one (create a link). I am using a regular expression pattern for finding the URLs (links) in the string. Check my example below:
Example :
<?php
// The Text you want to filter for urls
$text = "The text you want to filter goes here. http://google.com/abc/pqr
2The text you want to filter goes here. http://google.in/abc/pqr
3The text you want to filter goes here. http://google.org/abc/pqr
4The text you want to filter goes here. http://www.google.de/abc/pqr";
// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// Check if there is a url in the text
if (preg_match($reg_exUrl, $text, $url)) {
// make the urls hyper links
echo preg_replace($reg_exUrl, "<a href='.$url[0].'>" . $url[0] . "</a> ", $text);
} else {
// if no urls in the text just return the text
echo $text . "<br/>";
}
?>
But it is showing following output :
> The text you want to filter goes here. **http://google.com/abc/pqr** 2The
> text you want to filter goes here. **http://google.com/abc/pqr** 3The text
> you want to filter goes here. **http://google.com/abc/pqr** 4The text you
> want to filter goes here. **http://google.com/abc/pqr**
Whats wrong with this?
preg_replace_callbackif you're unversed with the placeholder syntax. Also there are existing "linkify" tools.