這是我目前擁有的代碼。我會如何調整它在每次返回的結果中包含http://
在href
?目前,http://
不在返回的結果中,除非它在原始字符串變量$text
中。如果它不在原來的$text
中,我希望將它添加到href
。謝謝!在這個PHP URL縮短代碼中包含「http://」
function urlfixer($text){
$pattern = '#\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))#';
$callback = create_function('$matches', '
$url = array_shift($matches);
$url_parts = parse_url($url);
$text = parse_url($url, PHP_URL_HOST) . parse_url($url, PHP_URL_PATH);
$text = preg_replace("/^www./", "", $text);
$last = -(strlen(strrchr($text, "/"))) + 1;
if ($last < 0) {
$text = substr($text, 0, $last) . "…";
}
return sprintf(\'<a rel="nofollow" href="%s">%s</a>\', $url, $text);
');
return preg_replace_callback($pattern, $callback, $text);
}
你不能只是將其追加到剛開始的時候,你從你的函數返回? – 2013-03-28 03:15:41