2011-08-09 46 views
0

HI我正在修改MyBB的源代碼。PHP preg_replace:下面的代碼是做什麼的?

下面的代碼是從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); 

它有什麼作用是什麼呢?我知道一點正則表達式,但這個有點太複雜。

有人可以向我解釋這一點嗎?

非常感謝!

回答

1
"#& -- 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供稿條目中編寫&符的安全方式。