[PHP] replacing \n with br

2002-09-06 Thread David T-G

Hi, all --

I have a php script which writes comments out to a file in the format

  field@@comment
  field@@comment
  ...

and everything has worked delightfully -- until someone put in a newline
in one of the items and now we have

  field@@com
  ment
  field@@comment
  ...

and things break.  I read the file, line by line, and parse on @@ as my
separator, so anything after a newline is lost.

I thought I'd just replace that newline with a br since that's what the
user really wants, but I can't get rid of the \n while I'm at it; through
various attempts at directly replacing or using variables or such like

  preg_replace(/\n/,br,$mydatastring) ;
  preg_replace(/$newline/,$linebreak,$mydatastring) ;

I get mixed results of

  field@@combr
  ment

or

  field@@com
  brment

but in no case have I managed to get

  field@@combrment

which is what I want.

Is there a way to turn off the recognition of \n as an end of line and
instead operate on the entire $mydatastring?


TIA  HAND

:-D
-- 
David T-G  * It's easier to fight for one's principles
(play) [EMAIL PROTECTED] * than to live up to them. -- fortune cookie
(work) [EMAIL PROTECTED]
http://www.justpickone.org/davidtg/Shpx gur Pbzzhavpngvbaf Qrprapl Npg!




msg78069/pgp0.pgp
Description: PGP signature


Re: [PHP] replacing \n with br

2002-09-06 Thread Robert Cummings

David T-G wrote:
 
I've never had problems with doing:

$foo = I don't like\nnewlines;

$foo = ereg_replace( \n, 'br /', $foo );

Cheers,
Rob.


 Hi, all --
 
 I have a php script which writes comments out to a file in the format
 
   field@@comment
   field@@comment
   ...
 
 and everything has worked delightfully -- until someone put in a newline
 in one of the items and now we have
 
   field@@com
   ment
   field@@comment
   ...
 
 and things break.  I read the file, line by line, and parse on @@ as my
 separator, so anything after a newline is lost.
 
 I thought I'd just replace that newline with a br since that's what the
 user really wants, but I can't get rid of the \n while I'm at it; through
 various attempts at directly replacing or using variables or such like
 
   preg_replace(/\n/,br,$mydatastring) ;
   preg_replace(/$newline/,$linebreak,$mydatastring) ;
 
 I get mixed results of
 
   field@@combr
   ment
 
 or
 
   field@@com
   brment
 
 but in no case have I managed to get
 
   field@@combrment
 
 which is what I want.
 
 Is there a way to turn off the recognition of \n as an end of line and
 instead operate on the entire $mydatastring?
 
 TIA  HAND
 
 :-D
 --
 David T-G  * It's easier to fight for one's principles
 (play) [EMAIL PROTECTED] * than to live up to them. -- fortune cookie
 (work) [EMAIL PROTECTED]
 http://www.justpickone.org/davidtg/Shpx gur Pbzzhavpngvbaf Qrprapl Npg!
 
   

Part 1.2Type: application/pgp-signature

-- 
.-.
| Robert Cummings |
:-`.
| Webdeployer - Chief PHP and Java Programmer  |
:--:
| Mail  : mailto:[EMAIL PROTECTED] |
| Phone : (613) 731-4046 x.109 |
:--:
| Website : http://www.webmotion.com   |
| Fax : (613) 260-9545 |
`--'

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




Re: [PHP] replacing \n with br

2002-09-06 Thread N. Pari Purna Chand

Bothe of us got the same problem at same time :-)

- Original Message - 
From: David T-G [EMAIL PROTECTED]
To: PHP General list [EMAIL PROTECTED]
Sent: Friday, September 06, 2002 9:28 PM
Subject: [PHP] replacing \n with br




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




Re: [PHP] replacing \n with br

2002-09-06 Thread David T-G

Rob, et al --

...and then Robert Cummings said...
% 
% David T-G wrote:
%  
% I've never had problems with doing:
% 
% $foo = I don't like\nnewlines;
% 
% $foo = ereg_replace( \n, 'br /', $foo );

I'm afraid I do.  Given my code

  $fin = stripslashes(${base64_encode($k)}) ;   ###
  print \$fin is .$fin.BR\n;###
  $fout = ereg_replace(\n,br,$fin) ;
  print and \$fout is .$fout.BR\n;  ###

(ignore the scary base64 stuff :-) I get

  $fin is .This is the first line. Here is the second line..
  and $fout is .This is the first line. 
  Here is the second line..

in my browser and

  $fin is .This is the first line.
  Here is the second line..BR
  and $fout is .This is the first line.
  brHere is the second line..BR
  brbr

in my browser's source window.  The newlines are still there.

Any thoughts?


% 
% Cheers,
% Rob.


Thanks  HAND

:-D
-- 
David T-G  * It's easier to fight for one's principles
(play) [EMAIL PROTECTED] * than to live up to them. -- fortune cookie
(work) [EMAIL PROTECTED]
http://www.justpickone.org/davidtg/Shpx gur Pbzzhavpngvbaf Qrprapl Npg!




msg78076/pgp0.pgp
Description: PGP signature


Re: [PHP] replacing \n with br

2002-09-06 Thread Robert Cummings

Actually on second thought... and I don't know why I went with the
crowd in the first place... I do the following:

$foo = I don't like\nnewlines;

$foo = str_replace( \n, 'br /', $foo );

That one work for you? It's more efficient too.

Cheers,
Rob.


David T-G wrote:
 
 Rob, et al --
 
 ...and then Robert Cummings said...
 %
 % David T-G wrote:
 % 
 % I've never had problems with doing:
 %
 % $foo = I don't like\nnewlines;
 %
 % $foo = ereg_replace( \n, 'br /', $foo );
 
 I'm afraid I do.  Given my code
 
   $fin = stripslashes(${base64_encode($k)}) ;   ###
   print \$fin is .$fin.BR\n;###
   $fout = ereg_replace(\n,br,$fin) ;
   print and \$fout is .$fout.BR\n;  ###
 
 (ignore the scary base64 stuff :-) I get
 
   $fin is .This is the first line. Here is the second line..
   and $fout is .This is the first line.
   Here is the second line..
 
 in my browser and
 
   $fin is .This is the first line.
   Here is the second line..BR
   and $fout is .This is the first line.
   brHere is the second line..BR
   brbr
 
 in my browser's source window.  The newlines are still there.
 
 Any thoughts?
 
 %
 % Cheers,
 % Rob.
 
 Thanks  HAND
 
 :-D
 --
 David T-G  * It's easier to fight for one's principles
 (play) [EMAIL PROTECTED] * than to live up to them. -- fortune cookie
 (work) [EMAIL PROTECTED]
 http://www.justpickone.org/davidtg/Shpx gur Pbzzhavpngvbaf Qrprapl Npg!
 
   

Part 1.2Type: application/pgp-signature

-- 
.-.
| Robert Cummings |
:-`.
| Webdeployer - Chief PHP and Java Programmer  |
:--:
| Mail  : mailto:[EMAIL PROTECTED] |
| Phone : (613) 731-4046 x.109 |
:--:
| Website : http://www.webmotion.com   |
| Fax : (613) 260-9545 |
`--'

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




Re: [PHP] replacing \n with br

2002-09-06 Thread David T-G

Rob, et al --

...and then Robert Cummings said...
% 
% Actually on second thought... and I don't know why I went with the

*grin*


% crowd in the first place... I do the following:
% 
% $foo = I don't like\nnewlines;
% 
% $foo = str_replace( \n, 'br /', $foo );
% 
% That one work for you? It's more efficient too.

Actually, no.  I thought about listing all of the things I had tried (and
ereg_replace wasn't one of 'em) in my post but didn't want to clutter
things up.  I've tried str_replace, preg_replace, and now ereg_replace,
and have tried working on the function extraction of the data directly as
well as the $fin and $fout separation.  I always have the newline left
behind.

I'm a [rusty] perl guy, and so I keep wanting to know what's the magic
variable in PHP to set to make it eat entire paragraphs, like $/ or such
in perl (there's where the rusty part comes in!).  If these functions are
just operating on a single line then the newline(s) won't get eaten.


% 
% Cheers,
% Rob.


Thanks  HAND

:-D
-- 
David T-G  * It's easier to fight for one's principles
(play) [EMAIL PROTECTED] * than to live up to them. -- fortune cookie
(work) [EMAIL PROTECTED]
http://www.justpickone.org/davidtg/Shpx gur Pbzzhavpngvbaf Qrprapl Npg!




msg78081/pgp0.pgp
Description: PGP signature


Re: [PHP] replacing \n with br

2002-09-06 Thread Robert Cummings

David T-G wrote:
 
 Rob, et al --
 
 ...and then Robert Cummings said...
 %
 % Actually on second thought... and I don't know why I went with the
 
 *grin*
 
 % crowd in the first place... I do the following:
 %
 % $foo = I don't like\nnewlines;
 %
 % $foo = str_replace( \n, 'br /', $foo );
 %
 % That one work for you? It's more efficient too.
 
 Actually, no.  I thought about listing all of the things I had tried (and
 ereg_replace wasn't one of 'em) in my post but didn't want to clutter
 things up.  I've tried str_replace, preg_replace, and now ereg_replace,
 and have tried working on the function extraction of the data directly as
 well as the $fin and $fout separation.  I always have the newline left
 behind.
 
 I'm a [rusty] perl guy, and so I keep wanting to know what's the magic
 variable in PHP to set to make it eat entire paragraphs, like $/ or such
 in perl (there's where the rusty part comes in!).  If these functions are
 just operating on a single line then the newline(s) won't get eaten.

*ACK* Stoopid microsoft... it's the \r character I bet.

Cheers,
Rob.
-- 
.-.
| Robert Cummings |
:-`.
| Webdeployer - Chief PHP and Java Programmer  |
:--:
| Mail  : mailto:[EMAIL PROTECTED] |
| Phone : (613) 731-4046 x.109 |
:--:
| Website : http://www.webmotion.com   |
| Fax : (613) 260-9545 |
`--'

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




Re: [PHP] replacing \n with br

2002-09-06 Thread Brent Baisley

Why not just use the nl2br() function? It's a lot easier that using any 
replace function you build yourself.


On Friday, September 6, 2002, at 11:58 AM, David T-G wrote:

 Hi, all --

 I have a php script which writes comments out to a file in the format

   field@@comment
   field@@comment
   ...

 and everything has worked delightfully -- until someone put in a newline
 in one of the items and now we have

   field@@com
   ment
   field@@comment
   ...

 and things break.  I read the file, line by line, and parse on @@ as my
 separator, so anything after a newline is lost.

 I thought I'd just replace that newline with a br since that's what 
 the
 user really wants, but I can't get rid of the \n while I'm at it; 
 through
 various attempts at directly replacing or using variables or such like

   preg_replace(/\n/,br,$mydatastring) ;
   preg_replace(/$newline/,$linebreak,$mydatastring) ;

 I get mixed results of

   field@@combr
   ment

 or

   field@@com
   brment

 but in no case have I managed to get

   field@@combrment

 which is what I want.

 Is there a way to turn off the recognition of \n as an end of line and
 instead operate on the entire $mydatastring?

 TIA  HAND

 :-D
 --
 David T-G  * It's easier to fight for one's 
 principles
 (play) [EMAIL PROTECTED] * than to live up to them. -- fortune 
 cookie
 (work) [EMAIL PROTECTED]
 http://www.justpickone.org/davidtg/Shpx gur Pbzzhavpngvbaf Qrprapl 
 Npg!

--
Brent Baisley
Systems Architect
Landover Associates, Inc.
Search  Advisory Services for Advanced Technology Environments
p: 212.759.6400/800.759.0577


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




RE: [PHP] replacing \n with br

2002-09-06 Thread Brian V Bonini

I'm sorry, I missed the first part of this thread so am only going by its
subject line mostly... But, I assume you are all aware of the nl2br function
and there's a special reason it does not apply here but I thought I'd
mention it any way... Just in case... :)

-B


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




[PHP] (SOLVED) Re: [PHP] replacing \n with br

2002-09-06 Thread David T-G

Rob, et al --

...and then Robert Cummings said...
% 
% David T-G wrote:
%  
%  ...and then Robert Cummings said...
%  %
...
%  % $foo = str_replace( \n, 'br /', $foo );
%  %
%  % That one work for you? It's more efficient too.
%  
%  Actually, no.  I thought about listing all of the things I had tried (and
...
%  well as the $fin and $fout separation.  I always have the newline left
%  behind.
...
% 
% *ACK* Stoopid microsoft... it's the \r character I bet.

That was it, or at least sort of.  Thank heavens I always test before I
post, because I was going to smugly comment that I'm using freebsd *but*
I tried switching to \r and noticed that the added br moved from one
line to the other.  So I whipped up

  $fout = ereg_replace((\n|\r)+,br,$fin) ;

and it works -- there must actually be \r\n there -- and I'll work
backwards from there (I'll use an expression like the above to robustly
handle by-god-anything that comes in!) to get rid of these temp variables.


% 
% Cheers,
% Rob.


Thanks a *bunch*!

:-D
-- 
David T-G  * It's easier to fight for one's principles
(play) [EMAIL PROTECTED] * than to live up to them. -- fortune cookie
(work) [EMAIL PROTECTED]
http://www.justpickone.org/davidtg/Shpx gur Pbzzhavpngvbaf Qrprapl Npg!




msg78087/pgp0.pgp
Description: PGP signature


Re: [PHP] replacing \n with br

2002-09-06 Thread David T-G

Brent, et al --

...and then Brent Baisley said...
% 
% Why not just use the nl2br() function? It's a lot easier that using any 

Because it doesn't do what I need.  From the Fine Manual:

  nl2br --  Inserts HTML line breaks before all newlines in a string
  string nl2br ( string string)
  Returns string with 'br /' inserted before all newlines.

I don't want to simply add a br; I want to *replace* the newline (and
return, as we found out in a subthread).


% replace function you build yourself.

Except that it doesn't work :-)


Thanks  HAND

:-D
-- 
David T-G  * It's easier to fight for one's principles
(play) [EMAIL PROTECTED] * than to live up to them. -- fortune cookie
(work) [EMAIL PROTECTED]
http://www.justpickone.org/davidtg/Shpx gur Pbzzhavpngvbaf Qrprapl Npg!




msg78088/pgp0.pgp
Description: PGP signature


Re: [PHP] replacing \n with br

2002-09-06 Thread David T-G

Brian --

...and then Brian V Bonini said...
% 
% I'm sorry, I missed the first part of this thread so am only going by its
% subject line mostly... But, I assume you are all aware of the nl2br function

Well, only sorta; I said replace in there, you may notice.


% and there's a special reason it does not apply here but I thought I'd
% mention it any way... Just in case... :)

Thanks anyway, but it doesn't work.  See the reply I just sent in this
thread for details.


% 
% -B


HAND

:-D
-- 
David T-G  * It's easier to fight for one's principles
(play) [EMAIL PROTECTED] * than to live up to them. -- fortune cookie
(work) [EMAIL PROTECTED]
http://www.justpickone.org/davidtg/Shpx gur Pbzzhavpngvbaf Qrprapl Npg!




msg78089/pgp0.pgp
Description: PGP signature


Re: [PHP] replacing \n with br

2002-09-06 Thread Brent Baisley

Sorry. Since you were looking to use br, I thought you were creating 
HTML content. But then, line feeds are ignored in HTML so then you 
wouldn't have a problem with them. I should have thought of that.

How are you reading the file? The key being that you said you read it 
line by line. Once you read a line, could you just grab the first 
length-1 characters and then do your parsing on that?


On Friday, September 6, 2002, at 12:46 PM, David T-G wrote:

 I don't want to simply add a br; I want to *replace* the newline (and
 return, as we found out in a subthread).
--
Brent Baisley
Systems Architect
Landover Associates, Inc.
Search  Advisory Services for Advanced Technology Environments
p: 212.759.6400/800.759.0577


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




Re: [PHP] replacing \n with br

2002-09-06 Thread David T-G

Brent --

...and then Brent Baisley said...
% 
% Sorry. Since you were looking to use br, I thought you were creating 
% HTML content. But then, line feeds are ignored in HTML so then you 

Well, yes and no.


% wouldn't have a problem with them. I should have thought of that.

Well, yes and no :-)  It's my app, and it's understandable that you don't
already know everything about it from having not seen it yet!


% 
% How are you reading the file? The key being that you said you read it 
% line by line. Once you read a line, could you just grab the first 
% length-1 characters and then do your parsing on that?

The data file format is

  field@@data
  field@@data
  ...

and a real example is

  comment@@this is a comment
  latdir@@N
  londir@@W
  latdec@@36
  londec@@89
  ...

and my code [now, after mods,] looks like

  ...
  if ( file_exists($filelocation) ) # do we have an info file?
  {
$newfile = fopen($filelocation,r);# grab it and start reading!
while ( !feof ($newfile) )  # (go only until the end, of 
course)
{
  $commentline = trim(fgets($newfile)); # get one line with whitespace 
trimmed
  if ( $commentline )   # a line with data?
  {
list ($field,$data) = preg_split(/@@/,$commentline);  # split it
$content[$field] = preg_replace(/br/,\n,$data) ;  # replace our swapped 
'br's with original '\n's and store it in the hash
  }
}
fclose($newfile);   # always lock up
  }
  ...

so I'm just using fgets to read it.  The reason it looks that way is
because, after the user has had the opportunity to edit the fields and
hits the button, I process the data as

  ...
$newfile = fopen($filelocation,w);
foreach ($keylist as $k)# loop thru keys
  { 
fwrite($newfile,$k@@.ereg_replace((\n|\r)+,br,stripslashes(${base64_encode($k)})).\n)
 ; } # write the clear text out
fclose($newfile);
  ...

Although I've solved the problem for now, the next version will probably
end the data with a @@ and so I can just read another line if I don't
have a closer.


Thanks again  HAND

:-D
-- 
David T-G  * It's easier to fight for one's principles
(play) [EMAIL PROTECTED] * than to live up to them. -- fortune cookie
(work) [EMAIL PROTECTED]
http://www.justpickone.org/davidtg/Shpx gur Pbzzhavpngvbaf Qrprapl Npg!




msg78115/pgp0.pgp
Description: PGP signature


Re: [PHP] replacing \n with br

2002-09-06 Thread Robert Cummings

David T-G wrote:
 
 Brent --
 
 The data file format is
 
   field@@data
   field@@data
   ...
 
 and a real example is
 
   comment@@this is a comment
   latdir@@N
   londir@@W
   latdec@@36
   londec@@89
   ...
 
 and my code [now, after mods,] looks like
 
   ...
   if ( file_exists($filelocation) ) # do we have an info file?
   {
 $newfile = fopen($filelocation,r);# grab it and start reading!
 while ( !feof ($newfile) )  # (go only until the end, of 
course)
 {
   $commentline = trim(fgets($newfile)); # get one line with 
whitespace trimmed
   if ( $commentline )   # a line with data?
   {
 list ($field,$data) = preg_split(/@@/,$commentline);  # split it
 $content[$field] = preg_replace(/br/,\n,$data) ;  # replace our 
swapped 'br's with original '\n's and store it in the hash
   }
 }
 fclose($newfile);   # always lock up
   }
   ...
 
 so I'm just using fgets to read it.  The reason it looks that way is
 because, after the user has had the opportunity to edit the fields and
 hits the button, I process the data as
 
   ...
 $newfile = fopen($filelocation,w);
 foreach ($keylist as $k)# loop thru keys
   { 
fwrite($newfile,$k@@.ereg_replace((\n|\r)+,br,stripslashes(${base64_encode($k)})).\n)
 ; } # write the clear text out
 fclose($newfile);
   ...
 
 Although I've solved the problem for now, the next version will probably
 end the data with a @@ and so I can just read another line if I don't
 have a closer.

Ignore me if this has already crossed your mind... but why not
use XML? You format would be extensible and there are utilities
that can process the input for you.

Cheers,
Rob.
-- 
.-.
| Robert Cummings |
:-`.
| Webdeployer - Chief PHP and Java Programmer  |
:--:
| Mail  : mailto:[EMAIL PROTECTED] |
| Phone : (613) 731-4046 x.109 |
:--:
| Website : http://www.webmotion.com   |
| Fax : (613) 260-9545 |
`--'

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