2016-03-26 30 views
0

特定のすべてのURL(mywebsite.com)をリンクに変換し、他のURLを@@@ spam @@@に変換するこの関数を書いています。PHP regexが特定のURLと一致するURLと一致するURL

function get_global_convert_all_urls($content) { 
    $content = strtolower($content); 
    $replace = "/(?:http|https)?(?:\:\/\/)?(?:www.)?(([A-Za-z0-9-]+\.)*[A-Za-z0-9-]+\.[A-Za-z]+)(?:\/.*)?/im"; 
    preg_match_all($replace, $content, $search); 
    $total = count($search[0]); 
    for($i=0; $i < $total; $i++) { 
    $url = $search[0][$i]; 
    if(preg_match('/mywebsite.com/i', $url)) { 
     $content = str_replace($url, '<a href="'.$url.'">'.$url.'</a>', $content);    
    } else { 
     $content = str_replace($url, '@@@[email protected]@@', $content); 
    } 
    } 

    return $content; 
} 

私が解決できない唯一の問題は、1行に2つのURLがある場合、正規表現がスペースで終わらないことです。

$content = "http://www.mywebsite.com/index.html http://www.others.com/index.html"; 

結果:

<a href="http://www.mywebsite.com/index.html">http://www.mywebsite.com/index.html</a> @@@[email protected]@@ 

私はこれを追加しようとしている(\ | S $)正規表現の結末が、運に:私は以下の結果を取得できますか

<a href="http://www.mywebsite.com/index.html http://www.others.com/index.html">http://www.mywebsite.com/index.html http://www.others.com/index.html</a> 

/(?:http|https)?(?:\:\/\/)?(?:www.)?(([A-Za-z0-9-]+\.)*[A-Za-z0-9-]+\.[A-Za-z]+)(?:\/.*)?(\s|$)/im 
+0

を私は 'のhref =" HTTP上記のリンクを考えます/ /www.mywebsite.com "も間違っています – RomanPerekhrest

+0

奇妙です...あなたの現在の正規表現 – RomanPerekhrest

+0

@RomanPerekhrest Oppsssを使用して、この結果「http://www.mywebsite.com @@@ spam @@@ ''を受け取りました。申し訳ありません、追加してみてください/index.h tml – richard

答えて

1

あなたの質問の変更に基づいて編集されました。

あなたの正規表現の最後に問題があります。そのため、より正確な表現で置き換えることをお勧めします。私はこれを本当に素早く調理しました。あなたのケースを検証するためのいくつかのテストが必要です。 =)

$matches = null; 
$returnValue = preg_match_all('!(?:http|https)?(?:\\:\\/\\/)?(?:www.)?(([A-Za-z0-9-]+\\.)*[A-Za-z0-9-]+\\.[A-Za-z]+)(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\\-\\._\\?\\,\\\'/\\\\\\+&%\\$#\\=~])*[^\\.\\,\\)\\(]!', 'mywebsite.com/index.html others.com/index.html', $matches); 

結果で:

array (
    0 => 
    array (
    0 => 'mywebsite.com/index.html ', 
    1 => 'others.com/index.html', 
), 
    1 => 
    array (
    0 => 'mywebsite.com', 
    1 => 'others.com', 
), 
    2 => 
    array (
    0 => '', 
    1 => '', 
), 
    3 => 
    array (
    0 => '', 
    1 => '', 
), 
    4 => 
    array (
    0 => 'l', 
    1 => 'm', 
), 
) 
+0

Oppsss ...申し訳ありません、両方のURLに/index.htmlを追加してください。 – richard

+0

ありがとう!出来た。 – richard

1

変更\S*への正規表現(?:\/.*)?の最後の要素。

あなたの正規表現はスペースを含む文字列の最後まで一致します。\S*はスペースではないすべての文字と一致します。

またに全体の正規表現を簡素化できます。

$replace = "~(?:https?://)?(?:www\.)?(([A-Z0-9-]+\.)*[A-Z0-9-]+\.[A-Z]+)\S*~im"; 
+0

ありがとう!それも働いた。 – richard

1

変更最後のURLセクション(/index.html/index.php)を捕捉するための正規表現パターンを。以下に示すように

/(?:http|https)?(?:\:\/\/)?(?:www.)?(([A-Za-z0-9-]+?\.)?[A-Za-z0-9-]+?\.?[A-Za-z]*?(\/\w+?\.\w+?)?)\b/im 
は、あなたの関数の内容を変更し

$content = "http://www.mywebsite.com/index.html http://www.others.com/index.html"; 

function get_global_convert_all_urls($content) { 
    $content = strtolower($content); 
    $replace = "/(?:http|https)?(?:\:\/\/)?(?:www.)?(([A-Za-z0-9-]+?\.)?[A-Za-z0-9-]+?\.?[A-Za-z]*?(\/\w+?\.\w+?)?)\b/im"; 
    preg_match_all($replace, $content, $search); 

    foreach ($search[0] as $url) { 
    if(preg_match('/mywebsite.com/i', $url)) { 
     $content = str_replace($url, '<a href="'.$url.'">'.$url.'</a>', $content);   
    } else { 
     $content = str_replace($url, '@@@[email protected]@@', $content); 
    } 
    } 

    return $content; 
} 

var_dump(get_global_convert_all_urls($content)); 

を出力://www.http:

string '<a href="http://www.mywebsite.com/index.html">http://www.mywebsite.com/index.html</a> @@@[email protected]@@' 
+0

ありがとう!それも働いた。 – richard

関連する問題