好的,使用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);
谢谢你们:)
答案 0 :(得分:0)
喜欢用
写的Removing redundant line breaks with regular expressions
您可以在阵列中添加'(?:(?:\ r \ n | \ r | \ n)\ s *){2} / s'和'' ..但我没有检查
确保ich只匹配新线两次
答案 1 :(得分:0)
这似乎是最简单的解决方案;用PHP完美地为我工作:
http://php.net/manual/en/function.preg-replace.php#85883
基本上你在正则表达式选择模式的开头加上“(* ANYCRLF)”。自PCRE版本7.3起支持
这里有更详细的解释:
http://en.wikipedia.org/wiki/Perl_Compatible_Regular_Expressions
(“换行/换行选项”部分)
答案 2 :(得分:-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);