php function nl2br () and Custom function nl2p () line-wrapping usage Analysis _php techniques

Source: Internet
Author: User
Tags closing tag trim

This article illustrates the use of the PHP function nl2br () and the Custom Function nl2p () line-wrapping. Share to everyone for your reference, specific as follows:

Working with stories

Many occasions we simply use textarea to get the user's long input without using an editor. User input line to "\ n" mode of storage, the output sometimes will not change lines, a large piece of text directly out. This is the time to wrap the text according to "\ n" in the library. PHP has its own function nl2br (), we can also customize the function nl2p ().

Let's look at the NL2BR () function.

Definitions and usage

The NL2BR () function inserts an HTML newline character (<br/>) before each new row (\ n) in the string.

A simple example:

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

The HTML code to run the result:

Welcome to <br/>
www.jb51.net

nl2p

NL2BR has a disadvantage, such as to use CSS to do paragraph indentation is more troublesome, this time need to nl2p. Replace the BR line with paragraph p, which is more simply a direct replacement:

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

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 is converted to HTML break tags.
 * @param boolean $xml When True, an XML self-closing tag is applied to break tags (<br/>). * @return string/function nl2p ($string, $line _breaks = true, $xml = True) {//Remove existing HTML formatting to AV OID double-wrapping things $string = str_replace (Array (' <p> ', ' </p> ', ' <br> ', ' <br/> '), ", $st
  Ring);
  It is conceivable this people might still want single line-breaks//without to new breaking. if ($line _breaks = = true) return ' <p> '. Preg_replace (Array ("/[\n]{2,})/I", "/([^>]) \ n ([^<])/I"), Array ("& Lt;/p>\n<p> ", ' <br '. ($xml = = true?) ' /' : '').' > '), Trim ($string)). '
  </p> '; else return ' <p> '. Preg_replace ("/([\n]{1,})/I", "</p>\n<p> ", Trim ($string)). '
</p> ';

 }

More interested in PHP related content readers can view the site topics: "PHP Operations and Operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Basic Grammar Introductory Tutorial", "PHP operation Office Document Skills Summary (including word,excel,access, PPT), "The PHP date and time usage summary", "PHP object-oriented Programming Introduction Course", "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.