1
function makeLinksInTheContent($html)
{
    $html= preg_replace("/(^|[\n ])([\w]*?)((ht|f)tp(s)?:\/\/[\w]+[^ \,\"\n\r\t<]*)/is", "$1$2<a href=\"$3\" rel=\"nofollow\" >$3</a>", $html);
    $html= preg_replace("/(^|[\n ])([\w]*?)((www|ftp)\.[^ \,\"\t\n\r<]*)/is", "$1$2<a href=\"http://$3\" rel=\"nofollow\" >$3</a>", $html);
    $html= preg_replace("/(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+)+)/i", "$1<a href=\"mailto:$2@$3\" rel=\"nofollow\">$2@$3</a>", $html);

    return($html);
}

これは私のコードです。

私の必要性は、URLを自動リンクすることです。preg_replace を使用して URL を検索し、この URL へのリンクを設定します。

例: 「ページに www.google.com が含まれています。」このコンテンツを makeLinksInTheContent($html) に渡すと、「A page contains www.google.com .」が返されます。

ただし、次の URL 形式はリンクされていません。

  1. ( http://www.google.com )

  2. www.test.com()and[]&^%$#@!+|!@#$%^&()_+}{:"?><,./;'[]=-09~`. co,in,com.com

  3. http://www.test.com()and[]&^%$#@!+|!@#$%^&()_+}{:"?><,./;'[]=- 09~`.co,in,com.com

  4. https://www.test.com()and[]&^%$#@!+|!@#$%^&()_+}{:"?><,./;'[]=- 09~`.co,in,com.com

  5. ftp://www.test.com()and[]&^%$#@!+|!@#$%^&()_+}{:"?><,./;'[]=- 09~`.co,in,com.com

私の正規表現にはいくつかの間違いがあると思います。私たちに提案してください。

4

2 に答える 2