Php form input box, line feed, carriage return, replacement _ PHP Tutorial

Source: Internet
Author: User
Tags php form
In the php form input box, press enter to replace the line feed. Some methods to replace line breaks in the form input box of php are summarized. if you need them, refer to this article. The code is as follows: Copy the code? Php? $ Strthisisatestn; $ pattenarra php summarizes some of the methods to replace line breaks and carriage returns in the form input box. if you need them, refer to this article.
The code is as follows:


? $ Str = "this is a test n ";
$ Patten = array ("rn", "n", "r ");
? // Replace rn first, then whether n exists, and then replace r
$ Str = str_replace ($ order, "", $ str );
?>

 

? // Php has three solutions

The code is as follows:

// 1. use str_replace to replace the line feed.
$ Str = str_replace (array ("rn", "r", "n"), "", $ str );

// 2. use regular expression replacement
$ Str = preg_replace ('// s */', '', $ str );

// 3. use the variables defined in php (recommended)
$ Str = str_replace (PHP_EOL, '', $ str );


? /*
* Get the line break of the user's operating system, n
* @ Access public
* @ Return string
*/
Function get_crlf ()
{
If (stristr ($ _ SERVER ['http _ USER_AGENT '], 'win '))
{
$ The_crlf = 'RN ';
}
Elseif (stristr ($ _ SERVER ['http _ USER_AGENT '], 'Mac '))
{
$ The_crlf = 'R'; // for old MAC OS
}
Else
{
$ The_crlf = 'n'; // The weight is larger.
}
Return $ the_crlf;
}

Test Code

The code is as follows:




PHP retrieves line breaks in form area data


$ Content = empty ($ _ POST ['content'])? Null: trim ($ _ POST ['content']);
If (! Empty ($ content) echo str_replace ("r", 'rl ', nl2br ($ content ));
Echo "r ".'
---------- Split line -------------------- '. "r ";
If (! Empty ($ content) echo str_replace ("n", 'nl ', nl2br ($ content ));
Echo "n ".'
---------- Split line -------------------- '. "n ";
If (! Empty ($ content) echo str_replace ("r", 'rl ', str_replace ("n", 'nl', nl2br ($ content )));
Echo "r ".'
---------- Split line ----------------------
'. "N ";
Echo 'hello'. "n". 'Boys! ';
Echo 'hello'. "r". 'Boys! ';
?>



1. the PHP function nl2br () is to insert an HTML line break before each new line (rn) in the string:
;
2. in Windows, the line feed is (rn );
3. in Notepad, r or n has the newline function;

Note: Use nl2br to change the line feed

Bytes. The code is as follows? Php? $ Str = this is a test n; $ patten = arra...

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.