PHP:使用正则表达式更改 url

发布于 2025-01-08 18:22:44 字数 390 浏览 1 评论 0原文

我正在寻找好的正则表达式,它可以将我的字符串从:

text text website.tld text text anotherwebsite.tld/longeraddress text http://maybeanotheradress.tld/file.ext

更改为 bbcodes

text text [url=website.tld]LINK[/url] text text [url=anotherwebsite.tld/longeradress]LINK[/url] text text [url=http://maybeanotheradress.tld/file/ext]LINK[/url]

您能提供建议吗?

I'm looking for nice regexp which could change me string from:

text text website.tld text text anotherwebsite.tld/longeraddress text http://maybeanotheradress.tld/file.ext

into bbcodes

text text [url=website.tld]LINK[/url] text text [url=anotherwebsite.tld/longeradress]LINK[/url] text text [url=http://maybeanotheradress.tld/file/ext]LINK[/url]

Could you please advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千纸鹤 2025-01-15 18:22:44

即使我投票支持重复,一般性建议:分而治之

在您的输入字符串中,所有“URL”都不包含任何空格。因此,您可以将字符串划分为不包含空格的部分:

$chunks = explode(' ', $str);

正如我们所知,每个部分现在都可能是一个链接,您可以创建自己的函数来说明这一点:

/**
 * @return bool
 */
function is_text_link($str)
{
    # do whatever you need to do here to tell whether something is
    # a link in your domain or not.

    # for example, taken the links you have in your question:

    $links = array(
        'website.tld', 
        'anotherwebsite.tld/longeraddress', 
        'http://maybeanotheradress.tld/file.ext'
    );

    return in_array($str, $links);
}

in_array 只是一个示例,您可能正在寻找基于正则表达式的模式匹配。您可以稍后对其进行编辑以满足您的需要,我将其保留为练习。

正如您现在可以说出链接是什么和不是什么一样,剩下的唯一问题是如何从链接创建 BBCode,这是一个相当简单的字符串操作:

 if (is_link($chunk))
 {
     $chunk = sprintf('[url=%s]LINK[/url]', $chunk);
 }

因此从技术上讲,所有问题都已解决,需要将其放在一起:

function bbcode_links($str)
{
    $chunks = explode(' ', $str);
    foreach ($chunks as &$chunk)
    {
        if (is_text_link($chunk))
        {
             $chunk = sprintf('[url=%s]LINK[/url]', $chunk);
        }              
    }
    return implode(' ', $chunks);
}

这已经与您有问题的示例字符串一起运行(演示):

$str = 'text text website.tld text text anotherwebsite.tld/longeraddress text http://maybeanotheradress.tld/file.ext';

echo bbcode_links($str);

输出:

text text [url=website.tld]LINK[/url] text text [url=anotherwebsite.tld/longeraddress]LINK[/url] text [url=http://maybeanotheradress.tld/file.ext]LINK[/url]

然后您只需要调整您的 is_link功能来满足您的需求。玩得开心!

Even I vote for duplicate, a general suggestion: Divide and Conquer.

In your input string, all "URLs" do not contain any spaces. So you can divide the string into the parts that do not contain spaces:

$chunks = explode(' ', $str);

As we know that each part is now potentially a link you can create your own function that is able to tell so:

/**
 * @return bool
 */
function is_text_link($str)
{
    # do whatever you need to do here to tell whether something is
    # a link in your domain or not.

    # for example, taken the links you have in your question:

    $links = array(
        'website.tld', 
        'anotherwebsite.tld/longeraddress', 
        'http://maybeanotheradress.tld/file.ext'
    );

    return in_array($str, $links);
}

The in_array is just an example, you might be looking for regular expression based pattern matching instead. You can edit it later to fit your needs, I leave this as an exercise.

As you can now say what a link is and what not, the only problem left is how to create a BBCode out of a link, that's a fairly simple string operation:

 if (is_link($chunk))
 {
     $chunk = sprintf('[url=%s]LINK[/url]', $chunk);
 }

So technically, all problems have been solved and this needs to be put together:

function bbcode_links($str)
{
    $chunks = explode(' ', $str);
    foreach ($chunks as &$chunk)
    {
        if (is_text_link($chunk))
        {
             $chunk = sprintf('[url=%s]LINK[/url]', $chunk);
        }              
    }
    return implode(' ', $chunks);
}

This already runs with your example string in question (Demo):

$str = 'text text website.tld text text anotherwebsite.tld/longeraddress text http://maybeanotheradress.tld/file.ext';

echo bbcode_links($str);

Output:

text text [url=website.tld]LINK[/url] text text [url=anotherwebsite.tld/longeraddress]LINK[/url] text [url=http://maybeanotheradress.tld/file.ext]LINK[/url]

You then only need to tweak your is_link function to fullfill your needs. Have fun!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文