从代码文件中删除所有空格的最简单方法? [英] Easiest way to remove all whitespace from a code file?

查看:47
本文介绍了从代码文件中删除所有空格的最简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在参加其中一项 Code Golf 比赛,您的文件越小越好.

I'm participating in one of the Code Golf competitions where the smaller your file size is, the better.

我正在寻找一个程序或网站,而不是手动删除所有空格等,它可以获取文件,删除所有空格(包括新行)并返回文件的紧凑版本.有什么想法吗?

Rather than manually removing all whitespace, etc., I'm looking for a program or website which will take a file, remove all whitespace (including new lines) and return a compact version of the file. Any ideas?

推荐答案

您可以使用:

sed 's/\s\s+/ /g' youfile > yourpackedfile`

还有这个在线工具.

你甚至可以用 PHP 来实现(生活是多么奇妙):

You can even do it in PHP (how marvelous is life):

$data = file_get_contents('foobar.php');
$data = preg_replace('/\s\s+/', ' ', $data);
file_put_contents('foobar2.php', $data);

您必须注意,这不会处理像 $bar = ' asd aa a'; 这样的字符串变量,这可能是一个问题,具体取决于您在做什么.在线工具似乎可以正确处理此问题.

You have to note this won't take care of a string variable like $bar = ' asd aa a'; it might be a problem depending on what you are doing. The online tool seems to handle this properly.

这篇关于从代码文件中删除所有空格的最简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆