0

Ok, reading a file using file_get_contents and removing <?php, <?, and/or any ?> tags, but I also want to remove all line breaks within the file. How can I achieve this?

For example a file like so:

<?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!';

?>

Should return this:

$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!';

But instead I am getting this written to the file, when using 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!';

With 2 line breaks above and 2 line breaks also below. How can I remove all of these line breaks?

Here is the preg_replace I am using to remove the php tags from within the file, but how can this be modified to also keep the file the way it is, and remove all line breaks?

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

Thanks guys :)

hakre
  • 193,403
  • 52
  • 435
  • 836
SoLoGHoST
  • 2,673
  • 7
  • 30
  • 51

3 Answers3

0

like written in

Removing redundant line breaks with regular expressions

you could probably add '(?:(?:\r\n|\r|\n)\s*){2}/s' and '' to your arrays ..but I did not check

just make sure ich matches new line twice only

Community
  • 1
  • 1
  • OMG, this is ALMOST what I need. cept it returned this: `$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 on just 1 line now. – SoLoGHoST Sep 08 '11 at 16:56
  • If I change `{2}` to `{3}` instead I still get extra lines within the code that separates each $txt variable, but the lines above and below are gone... Close but no cigar! – SoLoGHoST Sep 08 '11 at 17:01
  • Nevermind, this works a charm, cause it gets rid of ALL line breaks, so if you want any you have to replace it with "\n", CHEERS BRO and thanks for hunting down that topic, actually did a search for it myself, but was unsuccessful in finding anything that could help. Thanks :) – SoLoGHoST Sep 08 '11 at 17:06
0

This seems to be the simplest solution; works perfectly for me with PHP:

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

Basically you add "(*ANYCRLF)" to the beginning of your regex selection pattern. Supported since PCRE version 7.3


More detailed explanation here:

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

("Newline/linebreak options" section)

a20
  • 5,495
  • 2
  • 30
  • 27
-1

Better would be:

$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);
powtac
  • 40,542
  • 28
  • 115
  • 170
  • This is still putting line breaks in between each line within the file and still adds 2 lines above and 2 lines below the strings of the file when using `file_put_contents` to put it into another file. – SoLoGHoST Sep 08 '11 at 16:38
  • Also, I believe you have an error in your code. `$file` is not defined anywhere, yet you are using it. – SoLoGHoST Sep 08 '11 at 16:39
  • @SoLo, added a trim before the put usage, now the 2 lines before and after should be gone. Also fixed the usage of put! If there double line breaks in between the lines there is a problem with your source file. Then I assume there is some wired `\n\r` "Linux to Mac to Windows" problem! – powtac Sep 08 '11 at 16:50
  • Ok, sure bro, but your method did not help me any. definitely undefinable answered the question! Thanks anyways for your help! – SoLoGHoST Sep 08 '11 at 17:07