replacing spaces with newline [message #181307] |
Sat, 04 May 2013 11:09 |
Simon Hall
Messages: 2 Registered: May 2013
Karma: 0
|
Junior Member |
|
|
Hi Guys,
I am new to PHP and I am trying to clean up some text, what I am looking to do is to create a new line at each space, I have tried the following but it still outputs with just spaces.
<?php
$file = file_get_contents("mytext.txt");
$file = Str_replace(" ", "\r\n", $file);
echo $file;
?>
Any ideas?
Cheers
Simon
|
|
|
Re: replacing spaces with newline [message #181309 is a reply to message #181307] |
Sat, 04 May 2013 12:14 |
Denis McMahon
Messages: 634 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On Sat, 04 May 2013 04:09:06 -0700, Simon Hall wrote:
> $file = Str_replace(" ", "\r\n", $file);
Last time I checked, it was str_replace, not Str_replace. You don't need
the \r on all operating systems. Some operating systems that I understand
may require the \r may also use 0xa0 instead of 0x20 for a space under
some circumstances.
--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
|
|
|
Re: replacing spaces with newline [message #181310 is a reply to message #181307] |
Sat, 04 May 2013 12:20 |
bill
Messages: 310 Registered: October 2010
Karma: 0
|
Senior Member |
|
|
On 5/4/2013 7:09 AM, Simon Hall wrote:
> Hi Guys,
>
> I am new to PHP and I am trying to clean up some text,
what I am looking to do is to create a new line at each space,
I have tried the following but it still outputs with just spaces.
>
> <?php
>
> $file = file_get_contents("mytext.txt");
>
> $file = Str_replace(" ", "\r\n", $file);
>
> echo $file;
>
> ?>
>
> Any ideas?
>
>
> Cheers
>
>
> Simon
>
When displaying in a browser multiple spaces/tabs/newlines are
all rendered as 1 space. If you want the _display_ to treat
spaces as newlines use the html line break:
$file = Str_replace(" ", <br />", $file);
or keep what you have and use
$file = nl2br($file)
http://us2.php.net/manual/en/function.nl2br.php
bill
|
|
|
|
Re: replacing spaces with newline [message #181314 is a reply to message #181307] |
Sat, 04 May 2013 14:43 |
Simon Hall
Messages: 2 Registered: May 2013
Karma: 0
|
Junior Member |
|
|
Thanks guys, it was going to be going to a text file anyway. Was me just being an idiot the output was correctly formatted in a text file rather than the output of echo in the webpage.
Thank you for the responses, we live and learn :)
On Saturday, 4 May 2013 12:09:06 UTC+1, Simon Hall wrote:
> Hi Guys,
>
>
>
> I am new to PHP and I am trying to clean up some text, what I am looking to do is to create a new line at each space, I have tried the following but it still outputs with just spaces.
>
>
>
> <?php
>
>
>
> $file = file_get_contents("mytext.txt");
>
>
>
> $file = Str_replace(" ", "\r\n", $file);
>
>
>
> echo $file;
>
>
>
> ?>
>
>
>
> Any ideas?
>
>
>
>
>
> Cheers
>
>
>
>
>
> Simon
|
|
|