How does php convert the br line breaks in html to line breaks in text input?

Source: Internet
Author: User

PHP has a very good function: nl2br (), which converts the line feed in the text box to the HTML page <br/>, but how can we convert <br/> line breaks in html to line breaks in text boxes? The following methods can help you solve this problem.

PHP converts <br/> line breaks in html to line breaks in the text box:

function br2nl($text){    return preg_replace('/<br\\s*?\/??>/i','',$text);}

Or:

function br2nl($text){    $text=preg_replace('/<br\\s*?\/??>/i',chr(13),$text);return preg_replace('/&nbsp;/i',' ',$text);}

JS version converts <br/> line breaks in html to line breaks in the text box:

function br2nl(txt){    var re=/(<br\/>|<br>|<BR>|<BR\/>)/g;    var s=txt.replace(re,"\n");    return s;                                  }
Articles you may be interested in
  • Php converts all-round characters in a string to half-width characters.
  • PHP compresses html webpage code (clear spaces, line breaks, tabs, comment tags)
  • Php converts multiple consecutive spaces in a string into one.
  • Js restrictions: Only English letters and numbers can be entered. Chinese characters and other special characters cannot be entered.
  • PHP compresses html webpage code to reduce the amount of network data transmitted, clear spaces, tabs, and comment mark
  • A classic php tutorial book recommended for php beginners
  • Summary of methods for saving arrays as text formats in php
  • Php determines whether the string is full of English, pure Chinese, and a combination of Chinese and English

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.