note 119276 added to function.nl2br
| From: | JustinasM. | Date: | Mon, 02 May 2016 07:02:04 +0000 |
| Subject: | note 119276 added to function.nl2br | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-205412@lists.php.net to get a copy of this message | ||
This is example with "\R" regex token which matches any unicode newline character.
"u" flag treate strings as UTF-16. Which is optional, depending on your use case.
<?php
public function nl2br($string)
{
return preg_replace('/\R/u', '<br/>', $string);
}
?>
NOTE:
preg_replace versions are much slower than using str_replace version or built-in nl2br.
Check out pcre.backtrack_limit php.ini setting for information about PCRE limit. It's good to
know.
Some PHP7 benchmarks:
<?php
// nl2br()
function nl2br_str($string) {
return str_replace(["\r\n", "\r", "\n"], '<br/>',
$string);
}
function nl2br_preg_R($string)
{
return preg_replace('/\R/u', '<br/>', $string);
}
function nl2br_preg_rnnr($string)
{
return preg_replace('/(\r\n|\n|\r)/', '<br/>', $string);
}
?>
# nl2br
## Time: 0.02895712852478 s
# nl2br_str
## Time: 0.027923107147217 s
# nl2br_preg_R
## Time: 0.13350105285645 s
# nl2br_preg_rnnr
## Time: 0.14213299751282 s
----
Server IP: 72.52.91.14
Probable Submitter: 195.14.183.214
----
Manual Page -- http://php.net/manual/en/function.nl2br.php
Edit -- https://master.php.net/note/edit/119276
Del: integrated -- https://master.php.net/note/delete/119276/integrated
Del: useless -- https://master.php.net/note/delete/119276/useless
Del: bad code -- https://master.php.net/note/delete/119276/bad+code
Del: spam -- https://master.php.net/note/delete/119276/spam
Del: non-english -- https://master.php.net/note/delete/119276/non-english
Del: in docs -- https://master.php.net/note/delete/119276/in+docs
Del: other reasons-- https://master.php.net/note/delete/119276
Reject -- https://master.php.net/note/reject/119276
Search -- https://master.php.net/manage/user-notes.php