PHP newline symbol substitution and filtering example

Source: Internet
Author: User


First, directly filtered out

$reg = Array ("\ r \ n", "\ n", "\ R");
$replace = ";
$str =str_replace ($reg, $replace, $STR);

So the content will not have to wrap.

Two, become a line wrap

Var_dump ($STR) Print the content out, see in the browser:

String ' Hlmblog This is a magical website


Share It technology and summary


We're all here, and you ' (length=89)

Right click on the mouse click to view the source code of the page:


' Hlmblog This is a magical website & #10;& #10; sharing it technology and summarizing & #10;& #10; we're all here.
which


& #13;& #10; is an escape character for HTML that represents a carriage return line, respectively.

All we have to do is turn the multiple newline escape characters above into one, see the following code:


$str = Preg_replace ("/([\s]{2,})/", "\ n", $str);

Content into the following format, a number of newline successfully converted into a newline, in the mobile phone app and browser can be displayed normally.

1 ' Hlmblog This is a magical website & #10; sharing it technology and summarizing & #10; we're all here.
The browser can parse for <br> or escape tag characters, and if you want to convert the escape line inside the content into <br>, you can
Directly with NL2BR, but mobile phone app does not parse <br>, unless you do transfer processing.


NL2BR ("Hlmblog This is a magical website & #10; sharing it technology and summarizing & #10; we're all here, you.");
The NL2BR () function inserts an HTML newline character (<br>) before each new line (\ n) in the string

Related Article

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.