【发布时间】: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