php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login

Patch nl2br_additional_parameter for *General Issues Bug #64639

Patch version 2013-04-17 19:44 UTC

Return to Bug #64639 | Download this patch
Patch Revisions:

Developer: krakjoe@php.net

--- php-nts/ext/standard/string.c	2013-03-22 18:22:57.996671056 +0000
+++ php-fork/ext/standard/string.c	2013-04-17 20:42:57.108889157 +0100
@@ -4214,22 +4214,24 @@
 }
 /* }}} */
 
-/* {{{ proto string nl2br(string str [, bool is_xhtml])
+/* {{{ proto string nl2br(string str [, bool is_xhtml = true, [bool newlines = true]])
    Converts newlines to HTML line breaks */
 PHP_FUNCTION(nl2br)
 {
 	/* in brief this inserts <br /> or <br> before matched regexp \n\r?|\r\n? */
 	char		*tmp, *str;
-	int		new_length;
+	int		    new_length;
 	char		*end, *target;
-	int		repl_cnt = 0;
-	int		str_len;
+	int		    repl_cnt = 0;
+	int		    str_len;
 	zend_bool	is_xhtml = 1;
-
-	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &str, &str_len, &is_xhtml) == FAILURE) {
+    zend_bool   newlines = 1;
+    
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|bb", &str, &str_len, &is_xhtml, &newlines) == FAILURE) {
 		return;
 	}
 
+    
 	tmp = str;
 	end = str + str_len;
 
@@ -4247,7 +4249,6 @@
 			}
 			repl_cnt++;
 		}
-
 		tmp++;
 	}
 
@@ -4276,15 +4277,27 @@
 				}
 
 				*target++ = '>';
-
-				if ((*str == '\r' && *(str+1) == '\n') || (*str == '\n' && *(str+1) == '\r')) {
-					*target++ = *str++;
+				
+				if (newlines) {
+				    if ((*str == '\r' && *(str+1) == '\n') || (*str == '\n' && *(str+1) == '\r')) {
+					    *target++ = *str++;
+				    }
+				}  else {
+				
+				    if ((*str == '\r' && *(str+1) == '\n') || 
+				        (*str == '\n' && *(str+1) == '\r')) {
+					    new_length--;
+					    str++;
+				    }
+				    new_length--;
+				    str++;
+				    
+				    continue;
 				}
 				/* lack of a break; is intentional */
 			default:
 				*target++ = *str;
 		}
-
 		str++;
 	}
 
--- php-nts/ext/standard/basic_functions.c	2013-04-12 09:51:57.948788083 +0100
+++ php-fork/ext/standard/basic_functions.c	2013-04-17 20:35:22.610970155 +0100
@@ -2388,6 +2388,7 @@
 ZEND_BEGIN_ARG_INFO_EX(arginfo_nl2br, 0, 0, 1)
 	ZEND_ARG_INFO(0, str)
 	ZEND_ARG_INFO(0, is_xhtml)
+	ZEND_ARG_INFO(0, newlines)
 ZEND_END_ARG_INFO()
 
 ZEND_BEGIN_ARG_INFO_EX(arginfo_strip_tags, 0, 0, 1)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 02 22:01:30 2024 UTC