Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to remove new line characters from a file to make it compact  in PHP ?

Posted on 2009-12-16
12
Medium Priority
?
570 Views
Last Modified: 2013-12-13
How can I remove new line characters from a file in PHP ?

When I use ::

$result = str_replace('\n','',$str);

where $str is a file ::

$str = file_get_contents(filename);

and then echo the "result", the new line characters are still there.

Although I can remove spaces using above code.

0
Comment
Question by:puneetdudeja
  • 3
  • 3
  • 2
  • +3
12 Comments
 
LVL 15

Accepted Solution

by:
ludofulop earned 400 total points
ID: 26061299
try str_replace(Array("\n", "\r"), Array("",""), $str);
0
 
LVL 3

Assisted Solution

by:jagdishshirsat
jagdishshirsat earned 400 total points
ID: 26061302
hi,

You  are using LInux or windows system?

In linux.. new line character is identified by "\n\r".


Thanks
0
 
LVL 2

Author Comment

by:puneetdudeja
ID: 26061333
I have tried using "\n\r" but it does not work, i am using Linux, but the one above using array("\n","\r") worked.

ludofulop::
I want to know why the above one worked if you know ?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 26061337
You say "How can I remove new line characters from a file in PHP ?".

Well, if you mean a source file then you cannot do it with str_replace because that only does strings, not files. You can load an entire file into a string, do the change and then save it

$str = file_get_contents("/path/to/PHP/file");

$newStr = str_replace(array( "\r\n", "\n"), "", $str );

file_put_contents("/path/to/file", $newStr );

http://www.php.net/file_get_contents
http://www.php.net/file_put_contents

0
 
LVL 15

Expert Comment

by:ludofulop
ID: 26061352
@puneetudeja:

different operating systems have different line endings characters - line feed (\r) + new line (\n), or just one of them...
in y our case, you file has probably windows type line endings, consisting of both characters
0
 
LVL 2

Author Comment

by:puneetdudeja
ID: 26061383
That's what I want:: "To read a file into a string, then replace all new lines with nothing" and then search the replaced string for some patterns, because those patterns can be spread across multiple lines, and I think "preg_search" will search for patterns in separate lines.

e.g.

If I have ::

.contbox {

backcolor:grey;

}


and i want to search for this class and replace its color with black.

Then I have to search for a pattern which is spread across multiple lines, can I do it without removing new lines from the file ?
0
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 400 total points
ID: 26061410
preg_match will work across newlines if the multi-line flag is added, for example

preg_match('/[a-z]+/m', $data );

that "m" just after the pattern's closing / is the multi-line flag
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 26061421
0
 
LVL 5

Assisted Solution

by:Khalid Mehmood Awan
Khalid Mehmood Awan earned 400 total points
ID: 26061633
Steps:
Open the php file in Microsoft Word
Press Ctrl+H (alternatively, select Replace option in edit menu)
in "Find What" , type ^p
and in "replace with" type a space character
this means you are going to replace all new line characters with a space, making the php file Compact
Click Save and enjoy ;-)

Before you do it , make a copy of your formatted php file, so that you may use it later on...
0
 
LVL 5

Expert Comment

by:Khalid Mehmood Awan
ID: 26061638
to add more, some times the search for ^p fails , in that case try with ^l
0
 
LVL 5

Expert Comment

by:Khalid Mehmood Awan
ID: 26061687
this was for windows based files
and to clarify the characters i am telling
^ means the cap character, usually it can be written by pressing SHIFT+6

0
 
LVL 13

Assisted Solution

by:themrrobert
themrrobert earned 400 total points
ID: 26063869
This is the correct answer. Copy and paste the code. Not all PHP binaries treat '  ' and " " the same.

Using my code should work. Everytime. In all  versions of PHP and even between unix/windows or both.

$result = str_replace(array("\r","\n"),array('',''),$str);

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question