PHP中的正则表达式/找到“ ”

时间:2021-11-23 00:14:30

I want to check a textarea. If the user enter some links in the textarea, php should automatically tag the links. I'm using this code:

我想查一下textarea。如果用户在textarea中输入一些链接,php应该自动标记链接。我正在使用此代码:

    $message = "text with some link within"; 

    $url = '@(?!<a[^>]*?>)(http)?(s)?(://)?(([a-zA-Z])([-\w]+\.)+([^\s\.]+[^\s]*)+[^,.\s])(?![^<]*?</a>)@';


    if(preg_match($url, $message) == 1){

    $message = preg_replace($url, '<a href="http$2://$4" target="_blank" rel="nofollow" title="$0">$0</a>', $message);

    }

The problem is, when there's already a tagged link (with an "a" tag), regex is destroying the link.

问题是,当已经有标记链接(带有“a”标记)时,正则表达式正在破坏链接。

Here is an example:

这是一个例子:

first input from textarea: Hello .... test.com

textarea的第一个输入:你好.... test.com

changed by regex: Hello ... <a href="http://test.com" target="_blank" rel="nofollow" title="test.com">test.com</a>

由正则表达式更改:您好... test.com

this is working fine, but if you update this:

这工作正常,但如果你更新这个:

Hello ... http://test.com" target="_blank" rel="nofollow" title="test.com" target="_blank" rel="nofollow" title="test.com">test.com">test.com">test.com

Thanks for your help!

谢谢你的帮助!

1 个解决方案

#1


0  

I'm not familiar with PHP and maybe this is not a good pattern for url validation, but the point is if there is already an "a" tag, the text is not replaced.

我不熟悉PHP,也许这不是一个很好的url验证模式,但关键是如果已经有一个“a”标签,文本不会被替换。

<?php

    $message = array(
        'Hello ... <a href="http://test.com" target="_blank" rel="nofollow" title="test.com">test.com</a>',
        "Hello .... http://www.test.com ..."
    );

    $url = '@(<a[^>]*>[^<]+</a>|((https?://)?[\w\.-]+\.[a-zA-Z]{2,3}[^\s\W]*))@';

    foreach ($message as $msg) {

        preg_match($url, $msg, $matches);

        if(preg_match($url, $msg) == 1 && count($matches) > 2) {

            $msg = preg_replace($url, '<a href="$0" target="_blank" rel="nofollow" title="$0">$0</a>', $msg);

        }

        echo $msg.PHP_EOL;

    }

    // Output:
    // Hello ... <a href="http://test.com" target="_blank" rel="nofollow" title="test.com">test.com</a>
    // Hello .... <a href="http://www.test.com" target="_blank" rel="nofollow" title="http://www.test.com">http://www.test.com</a> ...

Hope it helps.

希望能帮助到你。

#1


0  

I'm not familiar with PHP and maybe this is not a good pattern for url validation, but the point is if there is already an "a" tag, the text is not replaced.

我不熟悉PHP,也许这不是一个很好的url验证模式,但关键是如果已经有一个“a”标签,文本不会被替换。

<?php

    $message = array(
        'Hello ... <a href="http://test.com" target="_blank" rel="nofollow" title="test.com">test.com</a>',
        "Hello .... http://www.test.com ..."
    );

    $url = '@(<a[^>]*>[^<]+</a>|((https?://)?[\w\.-]+\.[a-zA-Z]{2,3}[^\s\W]*))@';

    foreach ($message as $msg) {

        preg_match($url, $msg, $matches);

        if(preg_match($url, $msg) == 1 && count($matches) > 2) {

            $msg = preg_replace($url, '<a href="$0" target="_blank" rel="nofollow" title="$0">$0</a>', $msg);

        }

        echo $msg.PHP_EOL;

    }

    // Output:
    // Hello ... <a href="http://test.com" target="_blank" rel="nofollow" title="test.com">test.com</a>
    // Hello .... <a href="http://www.test.com" target="_blank" rel="nofollow" title="http://www.test.com">http://www.test.com</a> ...

Hope it helps.

希望能帮助到你。