0

この関数は文字列からリンクを作成しますが、問題は、すでにリンクがある場合、機能しないことです。たとえば、「私はリンクです」」ですが、「私はリンクhttp://www.google.com」で動作します。何か案が?

 function checkStringForLinks($string)
{
    /*** make sure there is an http:// on all URLs ***/
    //$string = preg_replace("/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i", "$1http://$2",$string);

    /*** make all URLs links ***/
    $string = preg_replace("/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i","<a target=\"_blank\" href=\"$1\">$1</A>",$string);

    /*** make all emails hot links ***/
    //$string = preg_replace("/([\w-?&;#~=\.\/]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?))/i","<A HREF=\"mailto:$1\">$1</A>",$string);

    return $string;
}

おっと、タイプが間違っています。つまり、\anchor\link\anchor\ に触れたくありません。形成されていないリンクのみです。

4

2 に答える 2

1

それを正しくするために、それはそれほど単純ではありません。そのための無料のソリューションがすでにあります。この単純なクラスlinkifyのように。

 $string = 'some text http://www.google.com <a href="http://www.google.com">google.com</a>';Some text http://www.google.com Some text <a href="http://www.google.com">google.com</a>
 echo Util::linkify($string);
 //returns 
 //Some text <a href="http://www.google.com">http://www.google.com</a> Some text <a   href="http://www.google.com">google.com</a>
于 2012-08-10T08:33:25.980 に答える
0
$string = preg_replace("/([\w]+\:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i","<a target=\"_blank\" href=\"$1\">$1</A>",$string);

: の前に \ を追加します。() と $1 は必要ありません。代わりに $0 を使用してください。

于 2012-08-10T08:33:27.150 に答える