我需要將單行註釋(//...)
轉換爲註釋(/*...*/)
。我在下面的代碼中幾乎完成了這個任務;然而,我需要這個函數來跳過任何一行註釋已經在塊註釋中。目前它與任何單行註釋匹配,即使單行註釋在塊註釋中也是如此。將單行註釋轉換爲註釋
## Convert Single Line Comment to Block Comments
function singleLineComments(&$output) {
$output = preg_replace_callback('#//(.*)#m',
create_function(
'$match',
'return "/* " . trim(mb_substr($match[1], 0)) . " */";'
), $output
);
}
那麼我不會擔心,如果$ x =「一些字符串//帶斜線」;變成$ x =「一些字符串/ *斜槓* /」;.這實際上是首選。另一方面,我添加了您所建議的更改並收到編譯錯誤。 警告:preg_replace_callback()[function.preg-replace-callback]:編譯失敗:lookbehind斷言在C:\ wamp \ www \ LessCSS \ Site \ cleaner \ inc \ util.php中的偏移量6處不是固定長度29 – roydukkey 2009-10-02 04:22:54
PHP的外觀只支持固定長度的斷言。這意味着你不能編寫一個匹配未定義字符數的後備式正則表達式,這將排除使用*和?。更多信息在這裏:http://www.php.net/manual/en/regexp.reference.assertions.php – 2009-10-02 04:39:12
謝謝你的擡頭。應該現在工作。 – 2009-10-02 16:25:15