2013-06-07 7 views
7

Helllo、私はウェブサイト上にユーザー生成のコンテンツをいくつか持っています。私はPHP関数を使用してそれからリンクを削除したい。例えばPHP。文字列からリンクを削除する

私は、次の文字列を持っている:。

"text1 http://link1 text2 www.link2 text3 link3.com text4" 

は、HTTP :, WWWを含む、単語を検出するための簡単な方法がある.COMとテキストからそれらを削除するには?または、リンクからテキストを消去する他の良い方法がありますか?

ありがとうございました!

答えて

10
$pattern = "/[a-zA-Z]*[:\/\/]*[A-Za-z0-9\-_]+\.+[A-Za-z0-9\.\/%&=\?\-_]+/i"; 
$replacement = ""; 
preg_replace($pattern, $replacement, $string); 
+0

これをチェックします – user2331090

+0

基本的な単純なURLでうまく動作します。 URLに{や}のような特殊文字が含まれていると失敗します。しかし、それはSEOのフレンドリーなリンクを削除するのに最適です。 – pkout

5

ああ、私は答えを見つけました。

function cleaner($url) { 
    $U = explode(' ',$url); 

    $W =array(); 
    foreach ($U as $k => $u) { 
if (stristr($u,'http') || (count(explode('.',$u)) > 1)) { 
    unset($U[$k]); 
    return cleaner(implode(' ',$U)); 
} 
} 
    return implode(' ',$U); 
} 

$url = "Here is another funny site www.tinyurl.com/55555 and http://www.tinyurl.com/55555 and img.hostingsite.com/badpic.jpg"; 
echo "Cleaned: " . cleaner($url); 
関連する問題