PHP preg_replace:下面的代码到底做了什么?
嗨,我正在修改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", "&$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从所有匹配中,我们获取
([^\#])
,在其前面加上&
并替换。它用于将所有
&xxx
序列替换为&xxx
,这是在 rss feed 项中编写 & 字符的安全方法。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.