From f358d036914408df2cfb75f4b9ff3128c0578ea7 Mon Sep 17 00:00:00 2001 From: Kalle Sommer Nielsen Date: Thu, 14 Aug 2008 02:55:13 +0000 Subject: [PATCH] Implemented feature request #34381 (nl2br() should have an option for XHTML/HTML compatible BR element) --- ext/standard/basic_functions.c | 1 + ext/standard/string.c | 37 +++++++++++++++++++++++----------- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 3111155a2c..e2e17c4307 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -2742,6 +2742,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 diff --git a/ext/standard/string.c b/ext/standard/string.c index 7a6d15fb15..9f08ab38dc 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -5978,19 +5978,20 @@ PHP_FUNCTION(hebrevc) } /* }}} */ -/* {{{ proto string nl2br(string str) U +/* {{{ proto string nl2br(string str [, bool is_xhtml]) U Converts newlines to HTML line breaks */ PHP_FUNCTION(nl2br) { - /* in brief this inserts
before matched regexp \n\r?|\r\n? */ - zstr str; - int str_len; - zend_uchar str_type; - zstr p, end, tmp, target; + /* in brief this inserts
or
before matched regexp \n\r?|\r\n? */ + zstr str; + int str_len; + zend_uchar str_type; + zstr p, end, tmp, target; int new_length; int repl_cnt = 0; + zend_bool is_xhtml = 1; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "t", &str, &str_len, &str_type) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "t|b", &str, &str_len, &str_type, &is_xhtml) == FAILURE) { return; } @@ -6038,7 +6039,11 @@ PHP_FUNCTION(nl2br) RETURN_ZSTRL(str_type, str, str_len, 1); } - new_length = str_len + repl_cnt * (sizeof("
") - 1); + if (is_xhtml) { + new_length = str_len + repl_cnt * (sizeof("
") - 1); + } else { + new_length = str_len + repl_cnt * (sizeof("
") - 1); + } if (str_type == IS_UNICODE) { tmp.u = target.u = eumalloc(new_length + 1); @@ -6051,8 +6056,12 @@ PHP_FUNCTION(nl2br) *target.u++ = (UChar) 0x3c /*'<'*/; *target.u++ = (UChar) 0x62 /*'b'*/; *target.u++ = (UChar) 0x72 /*'r'*/; - *target.u++ = (UChar) 0x20 /*' '*/; - *target.u++ = (UChar) 0x2f /*'/'*/; + + if (is_xhtml) { + *target.u++ = (UChar) 0x20 /*' '*/; + *target.u++ = (UChar) 0x2f /*'/'*/; + } + *target.u++ = (UChar) 0x3e /*'>'*/; if ((*p.u == (UChar) 0x0d /*'\r'*/ && *(p.u+1) == (UChar) 0x0a /*'\n'*/) @@ -6079,8 +6088,12 @@ PHP_FUNCTION(nl2br) *target.s++ = '<'; *target.s++ = 'b'; *target.s++ = 'r'; - *target.s++ = ' '; - *target.s++ = '/'; + + if (is_xhtml) { + *target.s++ = ' '; + *target.s++ = '/'; + } + *target.s++ = '>'; if ((*p.s == '\r' && *(p.s+1) == '\n') || (*p.s == '\n' && *(p.s+1) == '\r')) { -- 2.50.1