特定のすべての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
を私は 'のhref =" HTTP上記のリンクを考えます/ /www.mywebsite.com "も間違っています – RomanPerekhrest
奇妙です...あなたの現在の正規表現 – RomanPerekhrest
@RomanPerekhrest Oppsssを使用して、この結果「http://www.mywebsite.com @@@ spam @@@ ''を受け取りました。申し訳ありません、追加してみてください/index.h tml – richard