2012-11-02 42 views
1

我有一個小的功能,經過一堆文字,並期待在文本形式的任何URL,並將它們轉換成HTML的的提取文本:從一個鏈接

​​

我的功能如下:

function linkify($text) 
{ 
    $text = eregi_replace('(((f|ht){1}tp://)[[email protected]:%_\+.~#?&//=]+)', 
'<a target="_blank" href="\\1">\\1</a>', $text); 

    $text = eregi_replace('([[:space:]()[{}])(www.[[email protected]:%_\+.~#?&//=]+)', 
'\\1<a target="_blank" href="http://\\2">\\2</a>', $text); 

    return $text; 
} 

這是一切正常,但我在哪裏使用此功能,並打印出的HTML是在有限的寬度空間,有時鏈接最終大得多,然後可以適應空間,我最終溢出。

我想知道如何去做2件事:

a。從文本即去除不必要的「http://」,所以我最終會與

<a href="http://www.somewebsitelink.com">www.somewebsitelink.com</a> 

灣如果文本大於20個字符,請剪下它後面的所有內容並放入幾個點。 e.g:

<a href="http://www.somewebsitelink.com">www.somewebsitelin...</a> 

我想知道如果我有可能做到這一點,而無需使用正則表達式,但話又說回來我REG EXP的理解是相當有限的。

+1

我逼債覺得有這個防彈的解決方案,糾正我,如果我錯了。我只肯定eregi_replace不推薦(不推薦使用v5.3) – ajreal

+0

謝謝你 - 我不知道。 – willdanceforfun

回答

0

使用php substr函數。例如:

<?php substr($text,0,10);?> 
+0

沒有用,你在談論str_replace('http://','',$ text)? –

+0

編號我的意思是'$ text =「http://www.somewebsitelink.com」; $ text = substr($ text,0,10)或$ text = substr($ text,7,10);/* if http://不是必需的* /' – ujjwalwahi

2

我認爲這會做你所需要的。它只需要一個url作爲唯一的參數,並刪除前導'http:// www。',如果小於20個字符則返回字符串;如果超過20個字符,則返回前17個字符並追加'.. '

function get_formatted_url($url){ 
    $url = trim($url); 
    $url = str_replace("http://www.", "", strtolower($url)); 
    if(strlen($url) > 20){ 
     return substr($url, 0, 16) . '...'; 
    }else{ 
     return $url; 
    } 
} 

編輯:一個例子使用preg_replace_callback()

function get_formatted_url($url){ 
    $url = $url[0]; 
    $formatted_url = trim($url); 
    $formatted_url = str_replace("http://www.", "", strtolower($formatted_url)); 
    if(strlen($formatted_url) > 20){ 
     return '<a href="'.$url.'" />'. substr($formatted_url, 0, 16) . '... </a> '; 
    }else{ 
     return '<a href="'.$url.'" />'. $formatted_url . '</a> '; 
    } 
} 

function linkify($text){ 
    $reg = '(((f|ht)tp://)[[email protected]:%_\+.~#?&//=]+)'; 
    $text = preg_replace_callback($reg, "get_formatted_url", $text); 
    return $text; 
} 



$text = "abcd http://www.abc.com?hg=alkdjfa;lkdjfa;lkdjfa;lkdsjfa;ldks abcdefg http://www.abc.com"; 
echo linkify($text); 
+0

感謝你 - 它看起來像它會做的伎倆,如果我傳遞給該函數是一個URL,但我傳遞一個URL中的一堆文本 - 我可能沒有正確解釋 - 我已經更新了我的問題。 – willdanceforfun

+1

可能使用此方法preg-replace-callback而不是允許回調的eregi_replace。 http://php.net/manual/en/function.preg-replace-callback.php。 – Yellowledbet

2
$link = 'http://www.somewebsitelink.com'; 
function linkify($text, $maxLen = 15) 
{ 
    return preg_replace_callback('(((f|ht){1}tp://)([[email protected]:%_\+.~#?&//=]+))', function($t) use ($maxLen) { 
     if (strlen($t[3]) > $maxLen) 
     $t[3] = substr_replace($t[3], '...', $maxLen); 

     return sprintf('<a target="_blank" href="%s">%s</a>', $t[0], $t[3]); 
    }, $text); 
} 

header('content-type: text/plain'); 
echo linkify($link); 

代碼PHP < = 5.2

$link = 'http://www.somewebsitelink.com'; 
function linkify($text, $maxLen = 15) 
{ 
    $funcBody = <<<FUNC 
if (strlen(\$t[3]) > \$maxLen) 
    \$t[3] = substr_replace(\$t[3], '...', \$maxLen); 

return sprintf('<a target="_blank" href="%s">%s</a>', \$t[0], \$t[3]); 
FUNC; 
     $func = create_function(
     '$t, $maxLen =' . $maxLen, 
     $funcBody 
    ); 
    return preg_replace_callback('(((f|ht){1}tp://)([[email protected]:%_\+.~#?&//=]+))', $func, $text); 
} 

header('content-type: text/plain'); 
echo linkify($link); 

結果

<a target="_blank" href="http://www.somewebsitelink.com">www.somewebsite...</a> 
+0

看起來它可能是解決方案 - 但是當我通過它傳遞文本時,最終出現服務器錯誤。有任何想法嗎? – willdanceforfun

+0

它需要安裝[PCRE](http://www.php.net/manual/en/ref.pcre.php),但很可能你有舊的PHP(不超過5.3)版本,它不支持lambda函數。我已經更新了我的答案以適應舊版本的PHP。 – nkamm

+0

你說得對 - 我正在運行5.2。舊的解決方案絕對不是很好!感謝您的提供! – willdanceforfun