使用 [PHP] 从 JS / CSS 文件中删除注释

发布于 2024-10-08 12:21:58 字数 136 浏览 0 评论 0原文

我正在构建一个 PHP 脚本来缩小 CSS/Javascript,这(显然)涉及删除文件中的注释。有什么想法如何做到这一点? (最好,我需要去掉 /**/// 注释)

I'm building a PHP script to minify CSS/Javascript, which (obviously) involves getting rid of comments from the file. Any ideas how to do this? (Preferably, I need to get rid of /**/ and // comments)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

可遇━不可求 2024-10-15 12:21:58

JS 中删除注释的模式

$pattern = '/((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/';

CSS 中删除注释的模式

$pattern = '!/\*[^*]*\*+([^/][^*]*\*+)*/!'; 

$str = preg_replace($pattern, '', $str);

我希望上面的内容可以帮助某人..

REFF : http://castlesblog.com/2010/august/14/php-javascript-css-minification

Pattern for remove comments in JS

$pattern = '/((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/';

Pattern for remove comments in CSS

$pattern = '!/\*[^*]*\*+([^/][^*]*\*+)*/!'; 

$str = preg_replace($pattern, '', $str);

I hope above should help someone..

REFF : http://castlesblog.com/2010/august/14/php-javascript-css-minification

执着的年纪 2024-10-15 12:21:58

那个轮子已经被发明了——https://github.com/mrclay/minify

That wheel has been invented -- https://github.com/mrclay/minify.

那请放手 2024-10-15 12:21:58

请注意 - 以下方法并不适用于所有可能的情况。 在生产中使用之前进行测试

没有 preg 模式,没有任何类似的东西,这可以使用 PHP 内置的 TOKENIZER 轻松完成。所有这三个(PHP、JS 和 CSS)都共享在源文件中表示注释的相同方式,以及 PHP 的本机内置 token_get_all() 函数(不带 TOKEN_PARSE flag) 可以做肮脏的把戏,即使输入字符串不是格式良好的 PHP 代码,而这正是人们可能需要的。它所要求的只是字符串开头的 ,神奇的事情就发生了。 :)

<?php 
function no_comments (string $tokens)
{   // Remove all block and line comments in css/js files with PHP tokenizer.
    $remove = [];
    $suspects = ['T_COMMENT', 'T_DOC_COMMENT'];
    $iterate = token_get_all ('<?php '. PHP_EOL . $tokens);

    foreach ($iterate as $token) 
    {
        if (is_array ($token)) 
        {
            $name = token_name ($token[0]); 
            $chr = substr($token[1],0,1);

            if (in_array ($name, $suspects) 
            && $chr !== '#') $remove[] = $token[1];
        }
    }

    return str_replace ($remove, '', $tokens);
}

用法是这样的:

echo no_comments ($myCSSorJsStringWithComments);

PLEASE NOTE - the following approach will not work in all possible scenarios. Test before using in production.

Without preg patterns, without anything alike, this can be easily done with PHP built-in TOKENIZER. All three (PHP, JS and CSS as well) share the same way of representing comments in source files, and PHP's native, built-in token_get_all() function (without TOKEN_PARSE flag) can do dirty trick, even if the input string isn't well formed PHP code, which is exactly what one might need. All it asks is <?php at start of the string and magic happens. :)

<?php 
function no_comments (string $tokens)
{   // Remove all block and line comments in css/js files with PHP tokenizer.
    $remove = [];
    $suspects = ['T_COMMENT', 'T_DOC_COMMENT'];
    $iterate = token_get_all ('<?php '. PHP_EOL . $tokens);

    foreach ($iterate as $token) 
    {
        if (is_array ($token)) 
        {
            $name = token_name ($token[0]); 
            $chr = substr($token[1],0,1);

            if (in_array ($name, $suspects) 
            && $chr !== '#') $remove[] = $token[1];
        }
    }

    return str_replace ($remove, '', $tokens);
}

The usage goes something like this:

echo no_comments ($myCSSorJsStringWithComments);
私野 2024-10-15 12:21:58

查看缩小 ,“基于正则表达式的大量删除空格、不必要的注释和标记。”

Take a look at minify, a "heavy regex-based removal of whitespace, unnecessary comments and tokens."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文