php_strip_whitespace和trim的搭配使用

在学习kongphp框架时有这么一段代码是为了生成运行时文件的

    $runfile = RUNTIME_PATH.'_runtime.php';
    if(!is_file($runfile)) {
        $s  = trim(php_strip_whitespace(KONG_PATH.'base/base.func.php'), "<?ph>\r\n");
        $s .= trim(php_strip_whitespace(KONG_PATH.'base/core.class.php'), "<?ph>\r\n");
        ...$s = str_replace('defined(\'KONG_PATH\') || exit;', '', $s);
        file_put_contents($runfile, '<?php '.$s);
        unset($s);
    }
    include $runfile;

php_strip_whitespace 函数返回删除PHP注释以及空白字符的源代码文件。在确定一个PHP文件的实际代码量时这个函数很有用。

trim 函数的第二个参数是字符列表(charlist),可以将列表中列出的字符从文件的首尾位置中删除。

这里是删除首尾位置的“<”、“?”、“p”、“h”、“\r”、“\n” 这些字符,这样就过滤了文件首位的空白、换行、以及“<?php” 、“?>” ,剩下的为主程序字符串

将程序字符串连接起来赋予$s,由$s生成一个独立的文件,并包含该文件

posted @ 2016-01-21 17:08  MysticGrrrr  阅读(878)  评论(0编辑  收藏  举报