什么是从PHP文件中删除注释的最佳方法?
我想做类似strip-whitespace()的东西 - 但它也不应该删除换行符.
例如:
我要这个:
<?PHP
// something
if ($whatsit) {
do_something(); # we do something here
echo '<html>Some embedded HTML</html>';
}
/* another long
comment
*/
some_more_code();
?>
Run Code Online (Sandbox Code Playgroud)
成为:
<?PHP
if ($whatsit) {
do_something();
echo '<html>Some embedded HTML</html>';
}
some_more_code();
?>
Run Code Online (Sandbox Code Playgroud)
(虽然如果删除注释的空行仍然存在,那就不行了).
这可能是不可能的,因为需要保留嵌入式HTML - 这就是在谷歌上出现的东西绊倒了.
任何人都可以建议一个JSON解析器允许任何类型的注释,使用PHP绑定 - 需要严重的配置文件的评论,但json_decode不支持它们.
(我知道:1.其他格式如YAML,2.评论不属于标准的一部分)
更新:
我们为什么不使用:
YAML:基准测试显示速度较慢 - 我们可能希望通过网络发送数据 - 不确定YAML是否最适合.
XML:太冗长 - 简单的人工编辑是必需的.而且不需要XML的扩展功能.
INI:数据中存在可变深度的层次结构和嵌套.我们需要一种无处不在的格式,因为数据可能与应用程序一起分发,也可能与其他语言的应用程序一起使用.
预处理:数据可以由用户提供和共享,在向应用添加数据之前很难强制要求预处理.
如何使用PHP删除JS注释?这个问题已更新:2013年11月4日回答:Alexander Yancharuk但是现在有一个问题.新代码:id = id.replace(/\//g,'');
这是我的例子:
<?php
$output = "
//remove comment
this1 //remove comment
this2 /* remove comment */
this3 /* remove
comment */
this4 /* * * remove
* * * *
comment * * */
this5 http://removecomment.com
id = id.replace(/\//g,''); //do not remove the regex //
";
$output = preg_replace( "/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:)\/\/.*))/", "", $output ); //Yancharuk's code/regex
// "/(?<!\:)\/\/(.*)\\n/ = my oldest code
echo nl2br($output);
?>
Run Code Online (Sandbox Code Playgroud)
我的问题;
这是输出,最近:
this1
this2
this3
this4
this5 http://removecomment.com
ID = …
如何缩小Smarty中的所有输出HTML模板?
像这样:
$smarty->minify = true;
PS:我找到了{strip}功能,但是我应该在所有.tpl文件中使用此功能。我有很多.tpl文件,这种方式对我来说是不可能的。