Provides several ways to replace newline characters with PHP

Source: Internet
Author: User
Tags php form
    1. ? $str = "This is a test \ n";
    2. $patten = Array ("\ n", "\ n", "\ R");
    3. ?//replace \ r \ n First, then presence \ n
    4. $str =str_replace ($order, "", $str);
    5. ?>
Copy Code
    1. PHP has three ways to solve

    2. 1. Use Str_replace to replace line breaks
    3. $str = Str_replace (Array ("\ r \ n", "\ r", "\ n"), "", $str);

    4. 2. Use regular replacement

    5. $str = preg_replace ('//s*/', ' ', $str);

    6. 3. Use PHP-defined variables (recommended)

    7. $str = Str_replace (Php_eol, ", $STR);
Copy Code
    1. /*
    2. * Get the user's operating system line break, \ n
    3. * @access Public
    4. * @return String
    5. */
    6. function Get_crlf ()
    7. {
    8. if (Stristr ($_server[' http_user_agent '), ' Win ')
    9. {
    10. $the _crlf = ' \ r \ n ';
    11. }
    12. ElseIf (Stristr ($_server[' http_user_agent '), ' Mac ')
    13. {
    14. $the _crlf = ' \ r '; For-old MAC OS
    15. }
    16. Else
    17. {
    18. $the _crlf = ' \ n ';//The right to be significant
    19. }
    20. return $the _crlf;
    21. }
Copy Code

Note: When the foreground page is displayed, use NL2BR to change the line to

The second example illustrates:

Find an interesting thing to do:

    1. $text = "AAAA

    2. CCC ";

    3. $text =str_replace (' \ n ', "", $text);

    4. $text =str_replace (' \ R ', "", $text);
    5. $text =str_replace (' \ r \ n ', ' ", $text);

Copy Code

Normally, the above code should be able to replace line breaks, but in fact it is not. Very depressed, tried many times, is not working. Finally, change to this:

    1. $text =str_replace ("\ n", "", $text);
    2. $text =str_replace ("\ R", "", $text);
    3. $text =str_replace ("\ r \ n", "", $text);
Copy Code

Incredibly on it, the original is double quotes, single quotation mark problem!

Double quotation marks than single quote efficiency almost, because the double quotation marks in the process of PHP parsing, will also determine whether there are variables, single quotation marks will not have this judgment, so generally speaking, no variables involved in the case, I would use single quotation marks, did not expect this time to replace the newline character, with single quotation mark incredibly not

Finally written:

    1. $order = Array ("\ n", "\ n", "\ R");
    2. $replace = ";
    3. $text =str_replace ($order, $replace, $text);
Copy Code

This allows you to replace the line break. is not very convenient.

>>> You may be interested in the article:php stripped string newline instance parsing php compressed HTML (clear line breaks, clear tabs, remove comment marks) PHP form to convert textarea line breaks PHP regular filter HTML tags , spaces, line breaks, and so on. PHP methods for removing line breaks summarize PHP compressed HTML page code (clear spaces, line breaks, tabs, comment markers, etc.) PHP generates Excel and controls line breaks in Excel cells

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