开发者

PHP preg_replace: What does the following code do exactly?

开发者 https://www.devze.com 2023-03-27 12:34 出处:网络
HI I am modifying the source code of MyBB. The following code is from class_feedgeneration.php: /** * Sanitize content suitable for RSS feeds.

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($开发者_Go百科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", "&#x26;$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!


"#& -- 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 &#x26; and replace.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号