PHP function nl2br() and custom function nl2p() line break usage analysis_php skills

WBOY
Release: 2016-05-16 19:54:56
Original
1343 people have browsed it

The example in this article describes the use of PHP function nl2br() and custom function nl2p() to wrap. Share it with everyone for your reference, the details are as follows:

Usage scenarios

In many cases, we simply use textarea to obtain long input from users without using an editor. Line breaks for user input end with " " into the library, sometimes there will be no line breaks when outputting, and a large piece of text will come out directly. At this time, you can use the " "Wrap text. 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

The

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

A simple example:

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

Copy after login

HTML code of running results:

Welcome to <br />
www.jb51.net

Copy after login

nl2p

nl2br has a shortcoming. 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:

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

Copy after login

For more detailed functions, 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('<p>', '</p>', '<br>', '<br />'), '', $string);
  // It is conceivable that people might still want single line-breaks
  // without breaking into a new paragraph.
  if ($line_breaks == true)
    return '<p>'.preg_replace(array("/([\n]{2,})/i", "/([^>])\n([^<])/i"), array("</p>\n<p>", '<br'.($xml == true &#63; ' /' : '').'>'), trim($string)).'</p>';
  else 
    return '<p>'.preg_replace("/([\n]{1,})/i", "</p>\n<p>", trim($string)).'</p>';
}

Copy after login

Readers who are interested in more PHP-related content can check out the special topics on this site: "Summary of PHP operations and operator usage", "Summary of PHP network programming skills", " Introductory tutorial on PHP basic syntax", "Summary of PHP office document operation skills (including word, excel, access, ppt)", "Summary of PHP date and time usage》, "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php mysql database operation introductory tutorial" And "Summary of common database operation skills in PHP"

I hope this article will be helpful to everyone in PHP programming.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template