An Easy One for you all

Im sure this must be quite easy to accomplish but I can not find the
function/way to do it.

MySQL Database field is saved as....

"this is the text

and has line breaks

like this"

Now I want to concat each line into a variable like this.

"this is the text and has line breaks like this"

I have tried trim() which supposedly takes out whitespace but its not
working as I expect.

What do I have to do?

Thanks
MS [ Sa, 07 April 2007 17:42 ] [ ID #1681253 ]

Re: An Easy One for you all

MS wrote:
> Im sure this must be quite easy to accomplish but I can not find the
> function/way to do it.
>
> MySQL Database field is saved as....
>
> "this is the text
>
> and has line breaks
>
> like this"
>
> Now I want to concat each line into a variable like this.
>
> "this is the text and has line breaks like this"
>
> I have tried trim() which supposedly takes out whitespace but its not
> working as I expect.

trim removes whitespace before the first none-whitespace character in a
string, and all the whitespace after the last none-whitespace character in the
same string.

if you trim this string " a z ", you get "a z", you can have what ever kind
of whitespace charactes between a and z without them being removed.


> What do I have to do?

In this case eregi_replace could work, replace "\n" with a " " and you should
get one line. http://www.php.net/manual/en/function.eregi-replace.php

--

//Aho
Shion [ Sa, 07 April 2007 17:56 ] [ ID #1681254 ]

Re: An Easy One for you all

MS wrote:
> Im sure this must be quite easy to accomplish but I can not find the
> function/way to do it.
>
> MySQL Database field is saved as....
>
> "this is the text
>
> and has line breaks
>
> like this"
>
> Now I want to concat each line into a variable like this.
>
> "this is the text and has line breaks like this"
>
> I have tried trim() which supposedly takes out whitespace but its not
> working as I expect.
>
> What do I have to do?
>
> Thanks
>
>

<?php
$text="This is a line\r\nwith carriage\r\nreturns\r\nin it";
echo "<pre>$text</pre>";
$nocr=preg_replace("/\r\n/"," ",$text);
echo "<pre>$nocr</pre>";
?>
Tyno Gendo [ Sa, 07 April 2007 18:22 ] [ ID #1681255 ]

Re: An Easy One for you all

Tyno Gendo wrote:
[snip]
> <?php
> $text="This is a line\r\nwith carriage\r\nreturns\r\nin it";
> echo "<pre>$text</pre>";
> $nocr=preg_replace("/\r\n/"," ",$text);
> echo "<pre>$nocr</pre>";
> ?>

Just thought, you may not have the \r as that's more a DOS thing anyway,
Unix will have just the \n, so you would need:

$nocr=preg_replace("/\r*\n/"," ",$text);

ie. zero or more "\r" rather than specifying it will have a \r\n as in
my previous post.

i'm not the most ingenious at regex, so anyone else have any comments on
this that would be great ;)
Tyno Gendo [ Sa, 07 April 2007 18:27 ] [ ID #1681256 ]

Re: An Easy One for you all

"Tyno Gendo" <user [at] example.com> wrote in message
news:PyPRh.45914$kF3.14708 [at] fe1.news.blueyonder.co.uk...
> <?php
> $text="This is a line\r\nwith carriage\r\nreturns\r\nin it";
> echo "<pre>$text</pre>";
> $nocr=preg_replace("/\r\n/"," ",$text);
> echo "<pre>$nocr</pre>";
> ?>

Thanks Guys.

I had a Mind Block on this one !! I knew about the Trim() limits, except
for today !!
MS [ Sa, 07 April 2007 18:44 ] [ ID #1681257 ]

Re: An Easy One for you all

On Apr 7, 12:27 pm, Tyno Gendo <u... [at] example.com> wrote:
> Tyno Gendo wrote:
>
> [snip]
>
> > <?php
> > $text="This is a line\r\nwith carriage\r\nreturns\r\nin it";
> > echo "<pre>$text</pre>";
> > $nocr=preg_replace("/\r\n/"," ",$text);
> > echo "<pre>$nocr</pre>";
> > ?>
>
> Just thought, you may not have the \r as that's more a DOS thing anyway,
> Unix will have just the \n, so you would need:
>
> $nocr=preg_replace("/\r*\n/"," ",$text);
>
> ie. zero or more "\r" rather than specifying it will have a \r\n as in
> my previous post.
>
> i'm not the most ingenious at regex, so anyone else have any comments on
> this that would be great ;)

Why does everyone insist upon using regex's to do simple find/replace
operations?

str_replace("\n", " ", $someString);
zeldorblat [ Sa, 07 April 2007 19:52 ] [ ID #1681258 ]

Re: An Easy One for you all

ZeldorBlat wrote:
> On Apr 7, 12:27 pm, Tyno Gendo <u... [at] example.com> wrote:
[snip]
> Why does everyone insist upon using regex's to do simple find/replace
> operations?
>
> str_replace("\n", " ", $someString);
>

Yes, you can also do str_replace as you suggest, its up to personal
preference really. Me personally, I've been seeing regex in projects at
work until they come out of my ears so regex is no problem, but
admittedly they were for much more complex matching and replacing ;-)
Tyno Gendo [ Sa, 07 April 2007 21:49 ] [ ID #1681259 ]

Re: An Easy One for you all

"MS" <MS [at] nospam.com> wrote in message news:kI-dnT5M7Z06V4rbRVnyiQA [at] bt.com...
> "Tyno Gendo" <user [at] example.com> wrote in message
> news:PyPRh.45914$kF3.14708 [at] fe1.news.blueyonder.co.uk...
> > <?php
> > $text="This is a line\r\nwith carriage\r\nreturns\r\nin it";
> > echo "<pre>$text</pre>";
> > $nocr=preg_replace("/\r\n/"," ",$text);
> > echo "<pre>$nocr</pre>";
> > ?>
>
> Thanks Guys.
>
> I had a Mind Block on this one !! I knew about the Trim() limits, except
> for today !!
>
>
Don't you just hate that. I had a "Mind Block" in about 1996, and I
hope/expect it will end one day soon :)
vince
Vince Morgan [ So, 08 April 2007 03:40 ] [ ID #1681756 ]

Re: An Easy One for you all

"Vince Morgan" <vinhar [at] REMOVEoptusnet.com.au> wrote in message
news:4618490c$0$16558$afc38c87 [at] news.optusnet.com.au...
> "MS" <MS [at] nospam.com> wrote in message news:kI-dnT5M7Z06V4rbRVnyiQA [at] bt.com...
>> "Tyno Gendo" <user [at] example.com> wrote in message
>> news:PyPRh.45914$kF3.14708 [at] fe1.news.blueyonder.co.uk...
>> > <?php
>> > $text="This is a line\r\nwith carriage\r\nreturns\r\nin it";
>> > echo "<pre>$text</pre>";
>> > $nocr=preg_replace("/\r\n/"," ",$text);
>> > echo "<pre>$nocr</pre>";
>> > ?>
>>
>> Thanks Guys.
>>
>> I had a Mind Block on this one !! I knew about the Trim() limits, except
>> for today !!
>>
>>
> Don't you just hate that. I had a "Mind Block" in about 1996, and I
> hope/expect it will end one day soon :)

Stop taking acid! : )

-Lost
-Lost [ Mo, 09 April 2007 04:56 ] [ ID #1682468 ]
PHP » alt.php » An Easy One for you all

Vorheriges Thema: include/require dircetive problem
Nächstes Thema: decoding &#;