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...