php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #11904 ext/standard/string.c patch for nl2br()
Submitted: 2001-07-05 08:56 UTC Modified: 2001-09-09 13:03 UTC
From: reaster at comptechnews dot com Assigned: derick (profile)
Status: Closed Package: Unknown/Other Function
PHP Version: 4.0.6 OS: Linux 2.4.6 Slackware
Private report: No CVE-ID: None
 [2001-07-05 08:56 UTC] reaster at comptechnews dot com
I suggest the following patch for string.c to fix the 
nl2br() function.

Instead of replacing "\n" with "<br />\n", I think 
"\n<br>" is better.  For example, in a string, there might 
be a "\r\n" and when nl2br() is applied, it would become 
"\r<br />\n".  Having a "\r" by itself and not paired with 
a "\n" causes problems for me because most programs afaik 
expect either just a "\n" (unix systems) or "\r\n" 
(microsoft systems) and know how to deal with them.  But 
when a "\r" is by itself, strange things sometimes happen 
and the file will print corrupted.  But applying the patch 
below, "\r\n" would become "\r\n<br>" which keeps the 
carriage return and newline pair intact and adds the 
break... unix and windows programs understand the file 
still and html renderers don't care about formatting.

*** string.c.orig	Thu Jul  5 08:41:46 2001
--- string.c	Thu Jul  5 08:38:04 2001
***************
*** 2511,2517 ****

  	convert_to_string_ex(str);

! 	
php_char_to_str((*str)->value.str.val,(*str)->value.str.len,'\n',"<br 
/>\n",7,return_value);
  }
  /* }}} */

--- 2511,2517 ----

  	convert_to_string_ex(str);

! 	
php_char_to_str((*str)->value.str.val,(*str)->value.str.len,'\n',"\n<br>",5,return_value);
  }
  /* }}} */



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-07-05 10:41 UTC] derick@php.net
I'll commit this after I check the patch.

Derick
 [2001-07-05 12:56 UTC] vlad@php.net
Before committing it as it is, note that we are not using
<br>, but a <br /> now, and the patch seems to ignore that
fact. Aslo, shouldn't it be <br />\r\n instead for that matter?

Shouldn't we define what 'nl' in 'nl2br()' stands for? \r?
\n? \r\n? If just \n, then no patch needed. If any of the
three, then we need to handle \r\n properly, and also handle
\r somehow (just a bit more code).

Just a thought... I want to be consistent, so mac users
won't be unhappy, like it happened recently...
 [2001-07-06 13:39 UTC] reaster at comptechnews dot com
I see ... newline on Mac is just a carriage return '\r' 
(#000D). Newline on Unix is just line feed '\n' (#x000A).  
And then newline on Dos/Windows is carriage return line 
feed "\r\n".  This is more complicated if a file is 
treated as 16-bit unicode instead of ascii 8-bit and I'm 
NOT an expert on character encodings - other systems like 
mainframes use something called NEL (#x0085) to represent 
endofline.  The general handling of all these sequences is 
to convert them all to a plain line feed '\n'.

"\r\n" -> '\n'
'\r' -> '\n'
'\r'NEL -> '\n'
NEL -> '\n'

In the nl2br() function, before newline 2 br substitution 
occurs, a "newline normalization" can be added to:

1) replace all "\r\n" to '\n' (#xA)
2) replace all '\r' followed by [NEL] (if unicode?) to 
'\n' (#x000A)
3) replace all remaining (Mac?) '\r' to '\n'

Then do the actual nl2br sub replacing all '\n' with 
"\n<br>" (5 characters).

The general standard for a new line is '\n', too bad not 
all systems use just it.


 [2001-09-09 13:03 UTC] derick@php.net
Fixed in CVS, both \r\n, \r and \n work now.

Derick
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 05:01:29 2024 UTC