Home  >  Article  >  Backend Development  >  Line break usage and example analysis of PHP function nl2br() and custom function nl2p()

Line break usage and example analysis of PHP function nl2br() and custom function nl2p()

墨辰丷
墨辰丷Original
2018-06-04 10:33:551776browse

This article mainly introduces the use of PHP function nl2br() and custom function nl2p() for line wrapping. It combines examples to analyze the advantages and disadvantages of PHP function nl2br to implement line wrapping function and the usage skills of custom function nl2p line wrapping function. Required Friends can refer to the following

Usage scenarios

In many cases, we simply use textarea to obtain the user's long input without using an editor. The line breaks input by the user are stored in the form of "\n". Sometimes there is no line break when outputting, and a large piece of text comes out directly. At this time, you can wrap the text according to the "\n" in the library. PHP has its own function nl2br(), and we can also customize the function nl2p().

Let’s take a look at the nl2br() function first.

Definition and Usage

nl2br() function inserts an HTML newline character (df250b2156c434f3390392d09b1c9563 before each new line (\n) in a string) ).

A simple example:

<?php
$str = "Welcome to 
www.jb51.net";
echo nl2br($str);
?>

The HTML code of the running result:

Welcome to <br />
www.jb51.net

nl2p

nl2br has a disadvantage. For example, it is more troublesome to use CSS to indent paragraphs. In this case, nl2p is needed. Replace br line break with paragraph p line break. It is easier to replace it directly:

<?php
function nl2p($text) {
 return "<p>" . str_replace("\n", "</p><p>", $text) . "</p>";
}
?>

For a more detailed function, you can try:

/**
 * Returns string with newline formatting converted into HTML paragraphs.
 *
 * @param string $string String to be formatted.
 * @param boolean $line_breaks When true, single-line line-breaks will be converted to HTML break tags.
 * @param boolean $xml When true, an XML self-closing tag will be applied to break tags (<br />).
 * @return string
 */
function nl2p($string, $line_breaks = true, $xml = true)
{
  // Remove existing HTML formatting to avoid double-wrapping things
  $string = str_replace(array(&#39;<p>&#39;, &#39;</p>&#39;, &#39;<br>&#39;, &#39;<br />&#39;), &#39;&#39;, $string);
  // It is conceivable that people might still want single line-breaks
  // without breaking into a new paragraph.
  if ($line_breaks == true)
    return &#39;<p>&#39;.preg_replace(array("/([\n]{2,})/i", "/([^>])\n([^<])/i"), array("</p>\n<p>", &#39;<br&#39;.($xml == true ? &#39; /&#39; : &#39;&#39;).&#39;>&#39;), trim($string)).&#39;</p>&#39;;
  else 
    return &#39;<p>&#39;.preg_replace("/([\n]{1,})/i", "</p>\n<p>", trim($string)).&#39;</p>&#39;;
}

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

PHP method of sending AT commands and example code

phparray Function array_walk usage and examples

phpQuick sort principle and implementation method and example analysis

The above is the detailed content of Line break usage and example analysis of PHP function nl2br() and custom function nl2p(). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn