php 中转输入为链接

$hyperlinks = htmlspecialchars($hyperlinks);

function convert_hyperlinks($hyperlinks)
{
    // mention that '&' from $hyperliks passed from outside is coverted to &,
    // beacuse htmlspecialchars should be called on $hyperlinks before __FUNCTION__
    // but replace only switch & to '&' and hence ';' occured
    $hyperlinks = str_replace('&', '&', $hyperlinks);
    $hyperlinks = preg_replace('/((http:\/\/|https:\/\/|www\.)[-a-zA-Z0-9@:%_\+\.~#\?&\/=]+[^\.;,\s]?\b)/i', "<a href='$1' target='_blank'>$1</a>", $hyperlinks);   
 
    //change www. to http://www. in order to get external links
    $str = preg_replace('/href=(["|\'])www\./i', 'href=$1http://www.', $hyperlinks);
    $str = str_replace("\n", "<br>", $str);  
    return $str;
}

posted @ 2012-04-25 16:54  rereadyou  阅读(159)  评论(0编辑  收藏  举报