0

RegExプレーンテキストのURL(ハイパーリンクではない)を置き換えるためのこの優れた「小さな」ものに出くわしました。唯一の問題は、正規表現をほとんど知らないため、ブログでこれを機能させることに完全に行き詰まっていることです。

だから、私はURLを除外するのに助けを求めています。$exception_url = 'http://mysite.com'

function strip_urls($text, $xception_url = FALSE)
{
    return preg_replace("/( (?:
    (?:https?|ftp) : \\/*
    (?:
        (?: (?: [a-zA-Z0-9-]{2,} \\. )+
            (?: arpa | com | org | net | edu | gov | mil | int | [a-z]{2}
                | aero | biz | coop | info | museum | name | pro
                | example | invalid | localhost | test | local | onion | swift ) )
        | (?: [0-9]{1,3} \\. [0-9]{1,3} \\. [0-9]{1,3} \\. [0-9]{1,3} )
        | (?: [0-9A-Fa-f:]+ : [0-9A-Fa-f]{1,4} )
    )
    (?: : [0-9]+ )?
    (?! [a-zA-Z0-9.:-] )
    (?:
        \\/
        [^&?#\\(\\)\\[\\]\\{\\}<>\\'\\\"\\x00-\\x20\\x7F-\\xFF]*
    )?
    (?:
        [?#]
        [^\\(\\)\\[\\]\\{\\}<>\\'\\\"\\x00-\\x20\\x7F-\\xFF]+
    )?
) | (?:
    (?:
        (?: (?: [a-zA-Z0-9-]{2,} \\. )+
            (?: arpa | com | org | net | edu | gov | mil | int | [a-z]{2}
                | aero | biz | coop | info | museum | name | pro
                | example | invalid | localhost | test | local | onion | swift ) )
        | (?: [0-9]{1,3} \\. [0-9]{1,3} \\. [0-9]{1,3} \\. [0-9]{1,3} )
    )
    (?: : [0-9]+ )?
    (?! [a-zA-Z0-9.:-] )
    (?:
        \\/
        [^&?#\\(\\)\\[\\]\\{\\}<>\\'\\\"\\x00-\\x20\\x7F-\\xFF]*
    )?
    (?:
        [?#]
        [^\\(\\)\\[\\]\\{\\}<>\\'\\\"\\x00-\\x20\\x7F-\\xFF]+
    )?
) | (?:
    [a-zA-Z0-9._-]{2,} @
    (?:
        (?: (?: [a-zA-Z0-9-]{2,} \\. )+
            (?: arpa | com | org | net | edu | gov | mil | int | [a-z]{2}
                | aero | biz | coop | info | museum | name | pro
                | example | invalid | localhost | test | local | onion | swift ) )
        | (?: [0-9]{1,3} \\. [0-9]{1,3} \\. [0-9]{1,3} \\. [0-9]{1,3} )
    )
) )/Dx", '', $text);
}

答えていただければ幸いです、ありがとうございます。

4

2 に答える 2

2

正規表現を変更することはほとんど不可能であり、最終的には巨大になります。

ただし、URL として識別する例外 URL の部分を一時的にいくつかの偽の文字列に置き換えてから、正規表現の後にそれらを元に戻すことができます (そして、本当に偏執的になりたい場合は、置換文字列がそうでないことを確認できます)。テキストに既に存在しない (または URL を削除した後は存在しない) 場合は、存在しなくなるまで乱数を追加します):

$identifier = '.com';
$temp_replace = '@@@STRIP_URLS-COM@@@';
$identifier2 = '://';
$temp_replace2 = '@@@STRIP_URLS-SLASHES@@@';
if ($exception_url) {
    $text = str_replace($exception_url, str_replace(array($identifier, $identifier2), array($temp_replace, $temp_replace2), $exception_url), $text);
}

$text = preg_replace(...)
....rest of regex here...

if ($exception_url) {
    $text = str_replace(array($temp_replace, $temp_replace2), array($identifier, $identifier2), $text);
}
return $text;
于 2012-08-26T00:54:48.970 に答える
0

誰かがこれが便利だと確信しています。

相対 URL を指定して、サイトからの URL を許可できます。

strip_urls($blog_comment, 'http://www.mysite.com/');

パートナー ドメインのグループから:

strip_url($blog_comment, array('http://mysite.com/', 'http://partner.com/', 'http://partner1.com/')).

プレースホルダーを使用するという Mihai Loga のアイデアを使用して、配列または文字列を $exception_url として受け取るように最初のスクリプトを変更しました。また、より安全にするためにプレースホルダーを作成しました。

function strip_urls($text, $exception_url = array())
{
    if( ! empty($exception_url))
    {
    if(is_string($exception_url)) $exception_url = array($exception_url);

$placeholder_array = array();
$placeholder = md5(uniqid());

if(strpos($text, $placeholder))
{
    while(strpos($text, $placeholder))
    {
    $placeholder = md5(uniqid());
    }
}

for($i = 0; $i < count($exception_url); $i++)
{
    if( ! is_string($exception_url[$i]))
    {
    unset($exception_url[$i]);
    $exception_url = array_values($exception_url);
    continue;
    }

    $pos = mb_strpos($text, $exception_url[$i]);

    if (FALSE === $pos) continue;

    $text = substr_replace($text, $placeholder + $i, $pos, mb_strlen($exception_url[$i]));
    $placeholder_array[] = $placeholder + $i;
}
}

$text = preg_replace("/( (?:
    (?:https?|ftp) : \\/*
    (?:
        (?: (?: [a-zA-Z0-9-]{2,} \\. )+
            (?: arpa | com | org | net | edu | gov | mil | int | [a-z]{2}
                | aero | biz | coop | info | museum | name | pro
                | example | invalid | localhost | test | local | onion | swift ) )
        | (?: [0-9]{1,3} \\. [0-9]{1,3} \\. [0-9]{1,3} \\. [0-9]{1,3} )
        | (?: [0-9A-Fa-f:]+ : [0-9A-Fa-f]{1,4} )
    )
    (?: : [0-9]+ )?
    (?! [a-zA-Z0-9.:-] )
    (?:
        \\/
        [^&?#\\(\\)\\[\\]\\{\\}<>\\'\\\"\\x00-\\x20\\x7F-\\xFF]*
    )?
    (?:
        [?#]
        [^\\(\\)\\[\\]\\{\\}<>\\'\\\"\\x00-\\x20\\x7F-\\xFF]+
    )?
) | (?:
    (?:
        (?: (?: [a-zA-Z0-9-]{2,} \\. )+
            (?: arpa | com | org | net | edu | gov | mil | int | [a-z]{2}
                | aero | biz | coop | info | museum | name | pro
                | example | invalid | localhost | test | local | onion | swift ) )
        | (?: [0-9]{1,3} \\. [0-9]{1,3} \\. [0-9]{1,3} \\. [0-9]{1,3} )
    )
    (?: : [0-9]+ )?
    (?! [a-zA-Z0-9.:-] )
    (?:
        \\/
        [^&?#\\(\\)\\[\\]\\{\\}<>\\'\\\"\\x00-\\x20\\x7F-\\xFF]*
    )?
    (?:
        [?#]
        [^\\(\\)\\[\\]\\{\\}<>\\'\\\"\\x00-\\x20\\x7F-\\xFF]+
    )?
) | (?:
    [a-zA-Z0-9._-]{2,} @
    (?:
        (?: (?: [a-zA-Z0-9-]{2,} \\. )+
            (?: arpa | com | org | net | edu | gov | mil | int | [a-z]{2}
                | aero | biz | coop | info | museum | name | pro
                | example | invalid | localhost | test | local | onion | swift ) )
        | (?: [0-9]{1,3} \\. [0-9]{1,3} \\. [0-9]{1,3} \\. [0-9]{1,3} )
    )
) )/Dx", '', $text);

return (empty($exception_url))? $text : str_replace($placeholder_array, $exception_url, $text);

}

Mihai Loga とこれまでにこの正規表現を設計した功績... すべては良いアイデアから始まります。

于 2012-08-26T18:59:06.433 に答える