2017-06-12 57 views
1

如何获取链接文本(如降价)。说某一个类型的解析来自用户输入的链接和文本

结果会是什么样子:

<a href="https://google.com">Google</a> 

这是我当前的功能:

function MakeUrls($str) 
{ 
    $find=array('`((?:https?|ftp)://\S+[[:alnum:]]/?)`si','`((?<!//)(www\.\S+[[:alnum:]]/?))`si'); 

    $replace=array('<a href="$1" target="_blank"> $0</a>', '<a href="http://$1" target="_blank">$1</a>'); 

    return preg_replace($find,$replace,$str); 
} 

当有人类型https://google.com返回:

<a href="https://google.com" target="_blank"> https://google.com</a> 
+0

如果你乐于使用JavaScript有这样的:[现有的问题(https://stackoverflow.com/questions/7901760/how-can-i-get-the-title-这是一个网页给出的网址,一个外部网址使用jquer) – Moustachio

+0

这不是人。 –

回答

0
function makeURLs($str) { 
    $explode = explode(")[", $str); 
    if (sizeof($explode) !== 2) return false; //failsafe stuff 

    $name = substr($explode[0], 1, strlen($explode[0])); //remove the (
    $url = substr($explode[1], 0, strlen($explode[1])-1); //remove the ] 

    return "<a href=\"$url\" target=\"_blank\">$name</a>"; 
} 

应该这样做