PHP preg_replace:下面的代码到底做了什么?

发布于 2024-11-28 12:37:19 字数 687 浏览 0 评论 0原文

嗨,我正在修改MyBB的源代码。

以下代码来自class_feed Generation.php

/**
 * Sanitize content suitable for RSS feeds.
 *
 * @param  string The string we wish to sanitize.
 * @return string The cleaned string.
 */
function sanitize_content($content)
{
    $content = preg_replace("#&[^\s]([^\#])(?![a-z1-4]{1,10};)#i", "&$1", $content);
    $content = str_replace("]]>", "]]]]><![CDATA[>", $content);

    return $content;
}

第一个:

$content = preg_replace("#&[^\s]([^\#])(?![a-z1-4]{1,10};)#i", "&#x26;$1", $content);

它到底做了什么?我知道一点正则表达式,但是这个有点太复杂了。

有人可以向我解释一下吗?

多谢!

HI I am modifying the source code of MyBB.

The following code is from class_feedgeneration.php:

/**
 * Sanitize content suitable for RSS feeds.
 *
 * @param  string The string we wish to sanitize.
 * @return string The cleaned string.
 */
function sanitize_content($content)
{
    $content = preg_replace("#&[^\s]([^\#])(?![a-z1-4]{1,10};)#i", "&$1", $content);
    $content = str_replace("]]>", "]]]]><![CDATA[>", $content);

    return $content;
}

The 1st one:

$content = preg_replace("#&[^\s]([^\#])(?![a-z1-4]{1,10};)#i", "&$1", $content);

What does it do exactly? I know a little regex, but this one is a little too complicated.

Could some explain this to me?

Thanks a lot!

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

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

发布评论

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

评论(1

青丝拂面 2024-12-05 12:37:19
"#& -- the char & as is
[^\s] -- one not space character (also \S could be used instead)
([^\#]) -- one not-dash character
(?![a-z1-4]{1,10};) -- and negative lookahead assertion that previous chars
                    -- are not followed by chars in a-z1-4 range
                    -- (only 1 to 10 in a row) with ; after
#i" -- case insensitive

从所有匹配中,我们获取 ([^\#]),在其前面加上 & 并替换。

它用于将所有 &xxx 序列替换为 &xxx,这是在 rss feed 项中编写 & 字符的安全方法。

"#& -- the char & as is
[^\s] -- one not space character (also \S could be used instead)
([^\#]) -- one not-dash character
(?![a-z1-4]{1,10};) -- and negative lookahead assertion that previous chars
                    -- are not followed by chars in a-z1-4 range
                    -- (only 1 to 10 in a row) with ; after
#i" -- case insensitive

And from all the match we take ([^\#]), prepend it with & and replace.

It is used to replace all &xxx sequences with &xxx which is the safe way to write ampersand-char in rss feed item.

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