Re: [PHP] explode string at new line

2007-06-06 Thread Chris

Jim Lucas wrote:

Chris wrote:

Davi wrote:

Em Terça 05 Junho 2007 23:52, [EMAIL PROTECTED] escreveu:

That's exactly correct. Except I /think/ you should use \n instead of
'\n'.



Thank you for the reply... =)

I'll check this... BTW:

array explode ( string $delimiter, string $string [, int $limit] )

So, I was wrong...
The right way, probaly, is:

$str=explode(\n,$_POST[my_text]);


If it's coming from a textarea you'll want to use \r\n because a 
textarea uses both a carriage return (\r) and newline (\n) to separate.


Otherwise each element of the array will have a \r on the end which 
may end up causing you some issues later on.


I was wondering about this, I seem to recall that window/mac/*nix all do 
it differently.


With a little Google'ing I came across this.

http://www.sitepoint.com/forums/printthread.php?t=54074

This was written in 2002, so I am not sure about the Mac = \r.
Since they are using *nix now, it could be \n not sure

But the article had this to say:

Line breaks
People want to know how they can retain textarea line breaks in HTML. 
You should store text in the database in its original format (e.g. with 
just newlines) and then use nl2br() to convert newlines to HTML br / 
tags on display (thanks to the people here for teaching me that :)). 
That's all good, except for one problem with nl2br(): it doesn't seem to 
convert \r newlines (edit: this has now been fixed in PHP 4.2.0).


Windows uses \r\n newlines; *nix uses \n; Mac uses \r.

nl2br() works correctly on text from Windows/*nix because they contain 
\n. However, if you get text from a Mac, nl2br() will not convert its 
newlines (again, fixed in PHP 4.2.0). To remedy this, I use the 
following bit of code to convert \r\n or \r to \n before inserting it 
into the database. It won't hurt anything and ensures that nl2br() will 
work on the \n only newlines on display. Also, it has the side effect of 
saving 1 byte in the database per newline from Windows (by storing only 
\n instead of \r\n). :)


PHP Code:
$txt = preg_replace('/\r\n|\r/', \n, $txt);


Fair enough :)

I guess my thinking was since a textarea is a html 'standard' it would 
be consistent across all browsers/systems. I guess not :)


--
Postgresql  php tutorials
http://www.designmagick.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] explode string at new line

2007-06-06 Thread Paul Novitski

At 6/5/2007 10:50 PM, Jim Lucas wrote:

Windows uses \r\n newlines; *nix uses \n; Mac uses \r.

...

PHP Code:
$txt = preg_replace('/\r\n|\r/', \n, $txt);



Another way to write that PCRE pattern is /\r\n?/ (one carriage 
return followed by zero or one linefeed).


I recall also running into \n\r although I can't recall which system uses it.

As an alternative to PCRE, we can pass arrays to PHP's replace functions, e.g.:

$txt = str_replace(array(\r\n, \n\r, \r), \n, $txt);

Regards,

Paul
__

Paul Novitski
Juniper Webcraft Ltd.
http://juniperwebcraft.com 


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] explode string at new line

2007-06-06 Thread Davi
Em Quarta 06 Junho 2007 02:50, Jim Lucas escreveu:
 Chris wrote:
  Davi wrote:
  Em Terça 05 Junho 2007 23:52, [EMAIL PROTECTED] escreveu:
  That's exactly correct. Except I /think/ you should use \n instead of
  '\n'.
 
  array explode ( string $delimiter, string $string [, int $limit] )
 
  So, I was wrong...
  The right way, probaly, is:
 
  $str=explode(\n,$_POST[my_text]);
 
  If it's coming from a textarea you'll want to use \r\n because a
  textarea uses both a carriage return (\r) and newline (\n) to separate.
 
  Otherwise each element of the array will have a \r on the end which may
  end up causing you some issues later on.

 I was wondering about this, I seem to recall that window/mac/*nix all do
 it differently.

 http://www.sitepoint.com/forums/printthread.php?t=54074

 Line breaks
 People want to know how they can retain textarea line breaks in HTML.
 You should store text in the database in its original format (e.g. with
 just newlines) and then use nl2br() to convert newlines to HTML br /
 tags on display (thanks to the people here for teaching me that :)).
 That's all good, except for one problem with nl2br(): it doesn't seem to
 convert \r newlines (edit: this has now been fixed in PHP 4.2.0).

 PHP Code:
 $txt = preg_replace('/\r\n|\r/', \n, $txt);


Helped... =)

But... Why does it happen:

[code]

$object=mysql_fetch_object($result);

$texto = $object-texto;

$texto=preg_replace(/\r|\n/,,stripslashes($texto));
echo $texto;

[/code]

[output]

Teste
\r\nde formatação!
\r\nTudo funcionando...
\r\n

[/output]


I'm using stripslashse 'cause I'm getting the values from a DB.

Any kind of tip?

TIA


-- 
Davi Vidal
[EMAIL PROTECTED]
[EMAIL PROTECTED]
--
Religion, ideology, resources, land,
spite, love or just because...
No matter how pathetic the reason,
it's enough to start a war. 

Por favor não faça top-posting, coloque a sua resposta abaixo desta linha.
Please don't do top-posting, put your reply below the following line.



pgpXKjnY1odxa.pgp
Description: PGP signature


Re: [PHP] explode string at new line

2007-06-06 Thread Davi
Em Quarta 06 Junho 2007 10:54, Davi escreveu:
 But... Why does it happen:

 [code]

 $object=mysql_fetch_object($result);

 $texto = $object-texto;

 $texto=preg_replace(/\r|\n/,,stripslashes($texto));
 echo $texto;

 [/code]

 [output]

 Teste
 \r\nde formatação!
 \r\nTudo funcionando...
 \r\n

 [/output]


 I'm using stripslashse 'cause I'm getting the values from a DB.

 Any kind of tip?

 TIA

If I do:

[code]

echo \r\nJust testing... \r\n again...;

[/code]

The output is:

[output]

Just testing... again...

[/output]

And the source code (in browser) is:

[output]

Just testing...
again...

[/output]


Am I missing anything?

TIA

-- 
Davi Vidal
[EMAIL PROTECTED]
[EMAIL PROTECTED]
--
Religion, ideology, resources, land,
spite, love or just because...
No matter how pathetic the reason,
it's enough to start a war. 

Por favor não faça top-posting, coloque a sua resposta abaixo desta linha.
Please don't do top-posting, put your reply below the following line.



pgpCbBEc0rmeF.pgp
Description: PGP signature


Re: [PHP] explode string at new line

2007-06-06 Thread Jim Lucas

Davi wrote:

Em Quarta 06 Junho 2007 10:54, Davi escreveu:

But... Why does it happen:

[code]

$object=mysql_fetch_object($result);

$texto = $object-texto;

$texto=preg_replace(/\r|\n/,,stripslashes($texto));
echo $texto;

[/code]

[output]

Teste
\r\nde formatação!
\r\nTudo funcionando...
\r\n

[/output]


I'm using stripslashse 'cause I'm getting the values from a DB.

Any kind of tip?

TIA


If I do:

[code]

echo \r\nJust testing... \r\n again...;

[/code]

The output is:

how and where are you outputting this to?



[output]

Just testing... again...

[/output]

And the source code (in browser) is:

What OS?



[output]

Just testing...
again...

[/output]


Am I missing anything?

TIA




--
Jim Lucas

   Some men are born to greatness, some achieve greatness,
   and some have greatness thrust upon them.

Twelfth Night, Act II, Scene V
by William Shakespeare

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] explode string at new line

2007-06-06 Thread Davi
Em Quarta 06 Junho 2007 13:20, Jim Lucas escreveu:
 Davi wrote:
  Em Quarta 06 Junho 2007 10:54, Davi escreveu:
  But... Why does it happen:
 
  [code]
 
  $object=mysql_fetch_object($result);
 
  $texto = $object-texto;
 
  $texto=preg_replace(/\r|\n/,,stripslashes($texto));
  echo $texto;
 
  [/code]
 
  [output]
 
  Teste
  \r\nde formatação!
  \r\nTudo funcionando...
  \r\n
 
  [/output]
 
 
  I'm using stripslashse 'cause I'm getting the values from a DB.
 
 
  If I do:
 
  [code]
 
  echo \r\nJust testing... \r\n again...;
 
  [/code]
 
  The output is:

 how and where are you outputting this to?


How: echo
Where: Firefox 2.0.0.3

  [output]
 
  Just testing... again...
 
  [/output]
 
  And the source code (in browser) is:

 What OS?


SuSE 9.3 (Server)

Gentoo Linux 1.12.9 [2007.0] (Desktop/Development)

  [output]
 
  Just testing...
  again...
 
  [/output]
 
 

-- 
Davi Vidal
[EMAIL PROTECTED]
[EMAIL PROTECTED]
--
Religion, ideology, resources, land,
spite, love or just because...
No matter how pathetic the reason,
it's enough to start a war. 

Por favor não faça top-posting, coloque a sua resposta abaixo desta linha.
Please don't do top-posting, put your reply below the following line.



pgpKvupfZO86S.pgp
Description: PGP signature


Re: [PHP] explode string at new line

2007-06-05 Thread heavyccasey

That's exactly correct. Except I /think/ you should use \n instead of '\n'.

On 6/5/07, Davi [EMAIL PROTECTED] wrote:


Hi all.

I've the fowlling string:

$_POST[my_text]=hi...\nthis is my multi-line\ntext;

Can I use explode to have something like:

$str[0]=hi...;
$str[1]=this is my multi-line;
$str[2]=text;

$str=explode($_POST[my_text],'\n');


TIA and sorry the *very* poor english.


--
Davi Vidal
[EMAIL PROTECTED]
[EMAIL PROTECTED]
--
Religion, ideology, resources, land,
spite, love or just because...
No matter how pathetic the reason,
it's enough to start a war. 

Por favor não faça top-posting, coloque a sua resposta abaixo desta linha.
Please don't do top-posting, put your reply below the following line.





--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] explode string at new line

2007-06-05 Thread Davi
Em Terça 05 Junho 2007 23:52, [EMAIL PROTECTED] escreveu:
 That's exactly correct. Except I /think/ you should use \n instead of
 '\n'.


Thank you for the reply... =)

I'll check this... BTW:

array explode ( string $delimiter, string $string [, int $limit] )

So, I was wrong...
The right way, probaly, is:

$str=explode(\n,$_POST[my_text]);


Thank you very much.


-- 
Davi Vidal
[EMAIL PROTECTED]
[EMAIL PROTECTED]
--
Religion, ideology, resources, land,
spite, love or just because...
No matter how pathetic the reason,
it's enough to start a war. 

Por favor não faça top-posting, coloque a sua resposta abaixo desta linha.
Please don't do top-posting, put your reply below the following line.



pgpS3oEAG6jIw.pgp
Description: PGP signature


Re: [PHP] explode string at new line

2007-06-05 Thread Chris

Davi wrote:

Em Terça 05 Junho 2007 23:52, [EMAIL PROTECTED] escreveu:

That's exactly correct. Except I /think/ you should use \n instead of
'\n'.



Thank you for the reply... =)

I'll check this... BTW:

array explode ( string $delimiter, string $string [, int $limit] )

So, I was wrong...
The right way, probaly, is:

$str=explode(\n,$_POST[my_text]);


If it's coming from a textarea you'll want to use \r\n because a 
textarea uses both a carriage return (\r) and newline (\n) to separate.


Otherwise each element of the array will have a \r on the end which may 
end up causing you some issues later on.


--
Postgresql  php tutorials
http://www.designmagick.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] explode string at new line

2007-06-05 Thread Davi
Em Quarta 06 Junho 2007 00:18, Chris escreveu:
 Davi wrote:
  Em Terça 05 Junho 2007 23:52, [EMAIL PROTECTED] escreveu:
  That's exactly correct. Except I /think/ you should use \n instead of
  '\n'.
 
  So, I was wrong...
  The right way, probaly, is:
 
  $str=explode(\n,$_POST[my_text]);

 If it's coming from a textarea you'll want to use \r\n because a
 textarea uses both a carriage return (\r) and newline (\n) to separate.

 Otherwise each element of the array will have a \r on the end which may
 end up causing you some issues later on.


Yes. Is a textarea. Thank you very much! =D


-- 
Davi Vidal
[EMAIL PROTECTED]
[EMAIL PROTECTED]
--
Religion, ideology, resources, land,
spite, love or just because...
No matter how pathetic the reason,
it's enough to start a war. 

Por favor não faça top-posting, coloque a sua resposta abaixo desta linha.
Please don't do top-posting, put your reply below the following line.



pgpctEzLbMQgx.pgp
Description: PGP signature


Re: [PHP] explode string at new line

2007-06-05 Thread Jim Lucas

Chris wrote:

Davi wrote:

Em Terça 05 Junho 2007 23:52, [EMAIL PROTECTED] escreveu:

That's exactly correct. Except I /think/ you should use \n instead of
'\n'.



Thank you for the reply... =)

I'll check this... BTW:

array explode ( string $delimiter, string $string [, int $limit] )

So, I was wrong...
The right way, probaly, is:

$str=explode(\n,$_POST[my_text]);


If it's coming from a textarea you'll want to use \r\n because a 
textarea uses both a carriage return (\r) and newline (\n) to separate.


Otherwise each element of the array will have a \r on the end which may 
end up causing you some issues later on.


I was wondering about this, I seem to recall that window/mac/*nix all do 
it differently.


With a little Google'ing I came across this.

http://www.sitepoint.com/forums/printthread.php?t=54074

This was written in 2002, so I am not sure about the Mac = \r.
Since they are using *nix now, it could be \n not sure

But the article had this to say:

Line breaks
People want to know how they can retain textarea line breaks in HTML. 
You should store text in the database in its original format (e.g. with 
just newlines) and then use nl2br() to convert newlines to HTML br / 
tags on display (thanks to the people here for teaching me that :)). 
That's all good, except for one problem with nl2br(): it doesn't seem to 
convert \r newlines (edit: this has now been fixed in PHP 4.2.0).


Windows uses \r\n newlines; *nix uses \n; Mac uses \r.

nl2br() works correctly on text from Windows/*nix because they contain 
\n. However, if you get text from a Mac, nl2br() will not convert its 
newlines (again, fixed in PHP 4.2.0). To remedy this, I use the 
following bit of code to convert \r\n or \r to \n before inserting it 
into the database. It won't hurt anything and ensures that nl2br() will 
work on the \n only newlines on display. Also, it has the side effect of 
saving 1 byte in the database per newline from Windows (by storing only 
\n instead of \r\n). :)


PHP Code:
$txt = preg_replace('/\r\n|\r/', \n, $txt);

Hope this helps

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php