Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #9252 > unrolled thread

replacing spaces with newline

Started bySimon Hall <hall.simon82@gmail.com>
First post2013-05-04 04:09 -0700
Last post2013-05-04 07:43 -0700
Articles 5 — 4 participants

Back to article view | Back to comp.lang.php


Contents

  replacing spaces with newline Simon Hall <hall.simon82@gmail.com> - 2013-05-04 04:09 -0700
    Re: replacing spaces with newline Denis McMahon <denismfmcmahon@gmail.com> - 2013-05-04 12:14 +0000
    Re: replacing spaces with newline bill <william@TechServSys.com> - 2013-05-04 08:20 -0400
    Re: replacing spaces with newline Jeff North <jnorthau@yahoo.com.au> - 2013-05-04 22:24 +1000
    Re: replacing spaces with newline Simon Hall <hall.simon82@gmail.com> - 2013-05-04 07:43 -0700

#9252 — replacing spaces with newline

FromSimon Hall <hall.simon82@gmail.com>
Date2013-05-04 04:09 -0700
Subjectreplacing spaces with newline
Message-ID<6964ae80-5d55-44bf-984e-9277b42fc67d@googlegroups.com>
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

[toc] | [next] | [standalone]


#9254

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2013-05-04 12:14 +0000
Message-ID<km2u2l$ole$1@dont-email.me>
In reply to#9252
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@gmail.com

[toc] | [prev] | [next] | [standalone]


#9255

Frombill <william@TechServSys.com>
Date2013-05-04 08:20 -0400
Message-ID<km2udk$9cm$2@speranza.aioe.org>
In reply to#9252
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

[toc] | [prev] | [next] | [standalone]


#9257

FromJeff North <jnorthau@yahoo.com.au>
Date2013-05-04 22:24 +1000
Message-ID<t8v9o8dt5ve5mc4abqhnipaq2g6e4ntvps@4ax.com>
In reply to#9252
On Sat, 4 May 2013 04:09:06 -0700 (PDT), in comp.lang.php Simon Hall
<hall.simon82@gmail.com>
<6964ae80-5d55-44bf-984e-9277b42fc67d@googlegroups.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 );

[toc] | [prev] | [next] | [standalone]


#9259

FromSimon Hall <hall.simon82@gmail.com>
Date2013-05-04 07:43 -0700
Message-ID<887210fa-8f73-4a1e-a9a1-22768c93e5c6@googlegroups.com>
In reply to#9252
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web