]> granicus.if.org Git - php/commitdiff
MFH: Implemented feature request #34381 (nl2br() should have an option for XHTML...
authorKalle Sommer Nielsen <kalle@php.net>
Thu, 14 Aug 2008 02:56:23 +0000 (02:56 +0000)
committerKalle Sommer Nielsen <kalle@php.net>
Thu, 14 Aug 2008 02:56:23 +0000 (02:56 +0000)
NEWS
ext/standard/basic_functions.c
ext/standard/string.c

diff --git a/NEWS b/NEWS
index 3d041b4bbe3b6027074148990e15e44726c9ee84..a5925c6805ed923a42a7bc46f6997be40ccc2d60 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -12,6 +12,9 @@ PHP                                                                        NEWS
 - Added litespeed SAPI module. (George Wang)
 - Added ext/hash support to ext/session's ID generator. (Sara)
 
+- Implemented feature request #34381 (nl2br() should have an option for 
+  XHTML/HTML compatible BR element) (Kalle)
+
 - Fixed a bug causing miscalculations with the "last <weekday> of <n> month"
   relative time string. (Derick)
 
index b5c078a24bf2e75ae24127aa57cb0c542e463178..310b5c32465c29c736dc2ae24110d902cf232c98 100644 (file)
@@ -2739,6 +2739,7 @@ ZEND_END_ARG_INFO()
 static
 ZEND_BEGIN_ARG_INFO(arginfo_nl2br, 0)
        ZEND_ARG_INFO(0, str)
+       ZEND_ARG_INFO(0, is_xhtml)
 ZEND_END_ARG_INFO()
 
 static
index 5db9e85495560863e3331ef50f30887e0cf1c770..5bb1a81b6f0f70013bd96345d0b4d79161f6ca4e 100644 (file)
@@ -3887,18 +3887,19 @@ PHP_FUNCTION(hebrevc)
 }
 /* }}} */
 
-/* {{{ proto string nl2br(string str)
+/* {{{ proto string nl2br(string str [, bool is_xhtml])
    Converts newlines to HTML line breaks */
 PHP_FUNCTION(nl2br)
 {
-       /* in brief this inserts <br /> before matched regexp \n\r?|\r\n? */
-       char    *tmp, *str;
-       int     new_length;
-       char    *end, *target;
-       int     repl_cnt = 0;
-       int     str_len;
+       /* in brief this inserts <br /> or <br> before matched regexp \n\r?|\r\n? */
+       char            *tmp, *str;
+       int             new_length;
+       char            *end, *target;
+       int             repl_cnt = 0;
+       int             str_len;
+       zend_bool       is_xhtml = 1;
        
-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &str, &str_len, &is_xhtml) == FAILURE) {
                return;
        }
        
@@ -3927,7 +3928,12 @@ PHP_FUNCTION(nl2br)
                RETURN_STRINGL(str, str_len, 1);
        }
 
-       new_length = str_len + repl_cnt * (sizeof("<br />") - 1);
+       if (is_xhtml) {
+               new_length = str_len + repl_cnt * (sizeof("<br />") - 1);
+       } else {
+               new_length = str_len + repl_cnt * (sizeof("<br>") - 1);
+       }
+
        tmp = target = emalloc(new_length + 1);
 
        while (str < end) {
@@ -3937,8 +3943,12 @@ PHP_FUNCTION(nl2br)
                                *target++ = '<';
                                *target++ = 'b';
                                *target++ = 'r';
-                               *target++ = ' ';
-                               *target++ = '/';
+
+                               if (is_xhtml) {
+                                       *target++ = ' ';
+                                       *target++ = '/';
+                               }
+
                                *target++ = '>';
                                
                                if ((*str == '\r' && *(str+1) == '\n') || (*str == '\n' && *(str+1) == '\r')) {