Hướng dẫn dùng php newline trong PHP
(PHP 4, PHP 5, PHP 7, PHP 8) nl2br — Inserts HTML line breaks before all newlines in a string Descriptionnl2br(string $string, bool $use_xhtml = true): string ParametersstringThe input string. use_xhtmlWhether to use XHTML compatible line breaks or not. Return ValuesReturns the altered string. ExamplesExample #1 Using nl2br() echo nl2br("foo isn't\n bar"); The above example will output: Example #2 Generating valid HTML markup using the use_xhtml parameter
echo nl2br("Welcome\r\nThis is my HTML document", false); The above example will output: Welcome Example #3 Various newline separators $string = "This\r\nis\n\ra\nstring\r"; The above example will output: This See Also
CGameProgrammer at gmail dot com ¶ 17 years ago
It's important to remember that this function does NOT replace newlines with If you don't want newlines, do: $Result = str_replace( "\n",
' ngkongs at gmail dot com ¶ 15 years ago to replace all linebreaks to function nl2br2($string) { works perfect for me N/A ¶ 13 years ago Here's a more simple one: /** fquffio at live dot it ¶ 8 years ago Starting from PHP 4.3.10 and PHP 5.0.2, this should be the most correct way to replace You might also want to be "platform specific", and
therefore this function might be of some help: aabaev ¶ 3 years ago double quotes !== single quotes php > echo nl2br('\r\n'); Anders Norrbring ¶ 16 years ago Seeing all these suggestions on a br2nl function, I can also see that neither would work with a sloppy written html line break.. Users can't be trusted to write good code, we know that, and mixing case isn't too uncommon. I think this little snippet would do most tricks, both XHTML style and HTML, even mixed case like function br2nl($text) gautier dot michelin at gmail dot com ¶ 2 years ago This is a simple trick to transform newlines to paragraph a $string : $string_with_paragraphs = " " ", explode("\n", $string))." ";?> By exploding on new lines & adding end & begin for p, it's easy to concat with . You can bring it to a function : function nl2p($string) { " ", explode("\n", $string))." ";} ?> Collette ¶ 2 years ago Let's say a form text field does contain: To remove all the line breaks and split the input into an array: $input_from_text_field = preg_replace('#[\r\n]#','',nl2br($input_from_text_field,false)); $lines will now be an array where $lines[0] = "test line number 1" and so on. buzanits at gmail dot com ¶ 4 years ago If you write code that is to run in browser AND on the shell, this function could be useful. It uses PHP_EOL or function nl($string) { Three\r\nFour Five Six" . PHP_EOL); ?> leo dot mauro dot desenv at gmail dot com ¶ 6 years ago I test empirically this function nl2br and nl2br2 (create by ngkongs at gmail dot com). nl2br2 ~ 0.0000309944153 s The function nl2br2: Justinas M. ¶ 6 years ago This is example with "\R" regex token which matches any unicode newline character. public function nl2br($string) Some PHP7 benchmarks: function { return preg_replace('/\R/u', ' ', $string); } function nl2br_preg_rnnr($string){ return preg_replace('/(\r\n|\n|\r)/', ' ', $string); }?> # nl2br ## Time: 0.02895712852478 s # nl2br_str # nl2br_preg_R # nl2br_preg_rnnr chad at nobodyfamous dot ca ¶ 5 years ago
I
just spent a whole day trying to figure out why my textarea $_POST was not getting (INPUT_POST, 'text', FILTER_SANITIZE_SPECIAL_CHARS); ?> returns newlines as and so nl2br will miss them. rather use (INPUT_POST, 'text', FILTER_SANITIZE_STRING); ?> and newlines will remain intact so nl2br will pick them up. blacknine313 at gmail dot com ¶ 14 years ago After a recent post at the forums on Dev Shed, I noticed that it isn't mentioned, so I will mention it. nl2br returns pure HTML, so it should be after PHP anti-HTML functions ( such as strip_tags and htmlspecialchars ). darenschwenke at yahoo dot com ¶ 8 years ago This one works with br tags having attributes, in any case, /** j dot mons54 at gmail dot com ¶ 10 years ago for bbcode : $message = nl2br(preg_replace('#(\\]{1})(\\s?)\\n#Usi', ']', stripslashes($message))); hyponiq at gmail dot com ¶ 15 years ago On the contrary, mark at dreamjunky.comno-spam, this function is rightfully named. Allow me to explain. Although it does re-add the line break, it does so in an attempt to stay standards-compliant with the W3C recommendations for code format. According to said recommendations, a new line character must follow a line break tag. In this situation, the new line is not removed, but a break tag is added for proper browser display where a paragraph isn't necessary or wanted. |