php 有条件 preg_replace

php conditional preg_replace

我找到了一个正则表达式示例,它对我很有用。现在我需要修改它,但我对正则表达式非常糟糕。我搜索了 google 并尝试了一些但失败了。

这是我找到的正则表达式:

            $parsedMessage =
            preg_replace(
                array(
                    '/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))/',
                    '/(^|[^a-z0-9_])@([a-z0-9_]+)/i', '/(^|[^a-z0-9_])#([a-z0-9şŞıİçÇöÖüÜĞğ_]+)/i'),
                array(
                    '<a href="" target="_blank"></a>',
                    '<a href="/">@</a>',
                    '<a href="/hashtag/">#</a>'),
                $message);

它 returns $message 替换了 # hashtags,@ mentions 和正常的 links,现在我想添加这个(也找到了),

    return preg_replace(
    '/\s*[a-zA-Z\/\/:\.]*youtu(be.com\/watch\?v=|.be\/)([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i',
    "<iframe src=\"//www.youtube.com/embed/\" type=\"text/html\" width=\"480\" height=\"270\" allowfullscreen></iframe>",
    $string
);

就像你明白的那样,它 returns 取代了 youtube links 来嵌入代码。如果我这样使用它:

$parsedMessage =
        preg_replace(
            array('/\s*[a-zA-Z\/\/:\.]*youtu(be.com\/watch\?v=|.be\/)([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i',
                '/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))/',
                '/(^|[^a-z0-9_])@([a-z0-9_]+)/i', '/(^|[^a-z0-9_])#([a-z0-9şŞıİçÇöÖüÜĞğ_]+)/i'),
            array('<iframe src=\"//www.youtube.com/embed/\" type=\"text/html\" width=\"480\" height=\"270\" allowfullscreen></iframe>',
                '<a href="" target="_blank"></a>',
                '<a href="/">@</a>',
                '<a href="/hashtag/">#</a>'),
            $message);

Returns

但这不是我想要的。这是我需要的:

如果 $message 包含 youtube link,将 link 替换为 embed 并继续将普通 link 替换为 tag.Else,只需替换普通 link到

感谢您的帮助。

我解决了问题。如果你需要,你可以使用它:) Get it on github gist.

此处$message 是将被替换的文本。 (#hastags @mentions 和 http://youtube.com?v=blabla 被替换)

User::where('username', $m[2])->exists() 是 laravel 的一部分,你可以评论这个条件

$parsedMessage3 被替换文本。请注意,只允许一个 youtube link。

    function convert_clickable_links($message)
{

    global $youtube_link;
    global $count;

    $parsedMessage1 = preg_replace_callback(
        '/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))/',
        function ($m) use($youtube_link,$count)
        {
            $matches=[];

            $match_bool=preg_match
            (
                '/\s*[a-zA-Z\/\/:\.]*youtu(be.com\/watch\?v=|.be\/)([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i',
                $m[0],
                $matches
            );

            if($match_bool)
            {
                global $youtube_link;
                global $count;
                if($count>1)
                {
                    return '<a href="'.$m[1].'" target="_blank">'.$m[1].'</a>';
                }
                else
                {
                    $youtube_link='<iframe src="//www.youtube.com/embed/'.$matches[2].'" type="text/html" width="100%" height="270" allowfullscreen></iframe>';
                }
                ++$count;
                return  '';
            }
            else
            {
                return '<a href="'.$m[1].'" target="_blank">'.$m[1].'</a>';
            }
        },
        $message,-1,$count);

    $parsedMessage1=$parsedMessage1.$youtube_link;

    $parsedMessage2= preg_replace_callback(

        '/(^|[^a-z0-9_])@([a-z0-9_]+)/i',
        function ($m) {
            if (User::where('username', $m[2])->exists())
            {
                return $m[1].'<a href="/'.$m[2].'">@'.$m[2].'</a>';
            }
            else
            {
                return ' @'.$m[2];
            }
        },
        $parsedMessage1);

    $parsedMessage3=preg_replace('/(^|[^a-z0-9_])#([a-z0-9şŞıİçÇöÖüÜĞğ_]+)/i','<a href="/hashtag/">#</a>',$parsedMessage2);


    return $parsedMessage3;

}