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



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

以下代码来自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;
}

第一个

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

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

有人能给我解释一下吗?

非常感谢!

"#& -- 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

从所有的匹配中取([^#]),在其前面加上&#x26;并替换。

将所有的&xxx序列替换为&#x26;xxx,这是在rss提要条目中编写&字符的安全方法。

相关内容

  • 没有找到相关文章

最新更新