【问题标题】:PHP preg_replace to also remove line breaksPHP preg_replace 也删除换行符
【发布时间】:2011-11-13 03:47:33
【问题描述】:

好的,使用file_get_contents 读取文件并删除<?php、<? 和/或任何?> 标记,但我还想删除文件中的所有换行符。我怎样才能做到这一点?

例如这样的文件:

<?php

$txt['dpmod_testing'] = 'Testing';
$txt['dpmodtitle_testing'] = 'My Testing Module';
$txt['dpmodinfo_testing'] = 'Just a Testing Module, nothing special.';
$txt['dpmoddesc_testing'] = 'Here is a description for this testing module that you will see within the Add Modules section![color=red]I can give myself credit for this module if I want to![/color][hr]And this text string also accepts BBC Code!';

?>

应该返回这个:

$txt['dpmod_testing'] = 'Testing';
$txt['dpmodtitle_testing'] = 'My Testing Module';
$txt['dpmodinfo_testing'] = 'Just a Testing Module, nothing special.';
$txt['dpmoddesc_testing'] = 'Here is a description for this testing module that you will see within the Add Modules section![color=red]I can give myself credit for this module if I want to![/color][hr]And this text string also accepts BBC Code!';

但是,当我使用file_put_contents 时,我将其写入文件:

$txt['dpmod_testing'] = 'Testing';

$txt['dpmodtitle_testing'] = 'My Testing Module';

$txt['dpmodinfo_testing'] = 'Just a Testing Module, nothing special.';

$txt['dpmoddesc_testing'] = 'Here is a description for this testing module that you will see within the Add Modules section![color=red]I can give myself credit for this module if I want to![/color][hr]And this text string also accepts BBC Code!';

上面有 2 个换行符,下面也有 2 个换行符。如何删除所有这些换行符?

这是我用来从文件中删除 php 标签的 preg_replace,但是如何修改它以保持文件原样并删除所有换行符?

preg_replace(array('/<\?php/s', '/\?>/s', '/<\?/s'), array('', '', ''), $str);

谢谢大家:)

【问题讨论】:

  • 你是如何收到$str的?
  • 使用file_get_contents从文件中接收$str。
  • 我测试发现你的 preg_replace 函数没有问题,也许在其他地方有问题

标签: php preg-replace


【解决方案1】:

更好的是:

$content = file_get_contents('file.php');
$content = trim($content);
$content = ltrim($content, '<?php');
$content = ltrim($content, '<?');
$content = rtrim($content, '?>');
$content = trim($content);
file_put_contents('file.php', $content);

【讨论】:

  • 当使用file_put_contents 将其放入另一个文件时,这仍然会在文件中的每一行之间放置换行符,并且仍然会在文件的字符串上方添加 2 行,在下方添加 2 行。跨度>
  • 另外,我相信你的代码有错误。 $file 没有在任何地方定义,但你正在使用它。
  • @SoLo,在 put 用法之前添加了一个修剪,现在之前和之后的 2 行应该没有了。还修复了put的用法!如果两行之间有双换行符,则说明您的源文件有问题。然后我假设有一些有线\n\r“Linux 到 Mac 到 Windows”的问题!
  • 好的,兄弟,但是你的方法对我没有任何帮助。绝对无法定义回答了这个问题!无论如何感谢您的帮助!
【解决方案2】:

喜欢写的

Removing redundant line breaks with regular expressions

您可以将 '(?:(?:\r\n|\r|\n)\s*){2}/s' 和 '' 添加到数组中 ..但我没有检查

只要确保 ich 只匹配新行两次

【讨论】:

  • 天啊,这几乎是我需要的。除非它返回了这个:$txt['dpmod_testing'] = 'Russian Testing';$txt['dpmodtitle_testing'] = 'My Russian Testing Module';$txt['dpmodinfo_testing'] = 'Just a Russian Testing Module, nothing special.';$txt['dpmoddesc_testing'] = 'Here is a Russian description for this testing module that you will see within the Add Modules section![color=red]I can give myself credit for this module if I want to![/color][hr]And this text string also accepts BBC Code!'; ALL 现在只有 1 行。
  • 如果我将{2} 更改为{3},我仍然会在代码中获得额外的行来分隔每个$txt 变量,但是上面和下面的行已经消失了......关闭但没有雪茄!
  • 没关系,这很有魅力,因为它消除了所有换行符,所以如果你想要任何换行符,你必须用“\n”替换它,干杯兄弟,感谢你寻找这个话题,实际上自己进行了搜索,但未能找到任何可以提供帮助的东西。谢谢:)
【解决方案3】:

这似乎是最简单的解决方案;使用 PHP 非常适合我:

http://php.net/manual/en/function.preg-replace.php#85883

基本上,您将“(*ANYCRLF)”添加到正则表达式选择模式的开头。从 PCRE 7.3 版开始支持


这里有更详细的解释:

http://en.wikipedia.org/wiki/Perl_Compatible_Regular_Expressions

(“换行/换行选项”部分)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-01
    • 1970-01-01
    相关资源
    最近更新 更多