I have this function to replace a text with an url to url link. The callback is used to check if it has http or not in the link, if it has not, add http on it:
<?php
function toLink($titulo){
$url = '~(?:(https?)://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])~i';
$titulo = preg_replace_callback($url, function($matches) {
$url = $matches[0];
if (!preg_match('/^https?:\/\//', $url)) {
$url = 'http://'.$matches[0];
$url = '<a href="'.$url.'" target="_blank"
title="'.$url.'">'.$url.'</a>';
}
},$titulo);
return $titulo;
}
echo toLink("hi from www.google.com");
The return value is hi from where is my link?
return $urlseems like a good bet to me :-)function($matches) { ... }), after you've created the URL you want, return it