FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » replacing spaces with newline
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
replacing spaces with newline [message #181307] Sat, 04 May 2013 11:09 Go to next message
Simon Hall is currently offline  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 Go to previous messageGo to next message
Denis McMahon is currently offline  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 Go to previous messageGo to next message
bill is currently offline  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 #181312 is a reply to message #181307] Sat, 04 May 2013 12:24 Go to previous messageGo to next message
Jeff North is currently offline  Jeff North
Messages: 58
Registered: November 2010
Karma: 0
Member
On Sat, 4 May 2013 04:09:06 -0700 (PDT), in comp.lang.php Simon Hall
<hall(dot)simon82(at)gmail(dot)com>
<6964ae80-5d55-44bf-984e-9277b42fc67d(at)googlegroups(dot)com> 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?

If you are outputting to html then the \r\n are ignored so you could
use: echo nl2br( $file );
Re: replacing spaces with newline [message #181314 is a reply to message #181307] Sat, 04 May 2013 14:43 Go to previous message
Simon Hall is currently offline  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
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: take two - fetch items from a row.
Next Topic: Unique array of arrays - how can I do this?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 23 12:14:34 GMT 2024

Total time taken to generate the page: 0.01973 seconds