Several methods for replacing line breaks with php are provided

Source: Internet
Author: User
Several methods for replacing line breaks with php are provided

  1. ? $ Str = "this is a test \ n ";
  2. $ Patten = array ("\ r \ n", "\ n", "\ r ");
  3. ? // Replace \ r \ n first, check whether \ n exists, and finally replace \ r
  4. $ Str = str_replace ($ order, "", $ str );
  5. ?>

  1. // Php has three solutions

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

  4. // 2. use regular expression replacement

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

  6. // 3. use the variables defined in php (recommended)

  7. $ Str = str_replace (PHP_EOL, '', $ str );

  1. /*
  2. * Get the line break of the user's operating system, \ 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 weight is larger.
  19. }
  20. Return $ the_crlf;
  21. }

Note: Use nl2br to change the line feed

Example 2:

Find an interesting thing:

  1. $ Text = "aaaa

  2. Ccc ";

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

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

Normally, the above code should be able to replace line breaks, but in fact it is not. Very depressed. I tried it many times, but it didn't work. Finally, change it to the following:

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

This is actually enough. it turns out to be a double quotation mark, which is a single quotation mark!

Double quotation marks are less efficient than single quotation marks, because during the process of being parsed by php, double quotation marks will also determine whether there is a variable in it, and single quotation marks won't have this judgment, so generally speaking, without variables involved, I would always use single quotes. I didn't expect this time to replace line breaks, but it wouldn't work if I used single quotes ·····

Last written:

  1. $ Order = array ("\ r \ n", "\ n", "\ r ");
  2. $ Replace = '';
  3. $ Text = str_replace ($ order, $ replace, $ text );

In this way, you can replace the line break. Is it very convenient.

>>> Articles you may be interested in: php removes the string line break instance parsing php compresses html (clear line breaks, clear tabs, remove comment tags) php form conversion textarea linefeed method php regular filter html tags, spaces, line breaks and other code examples php remove linefeed method summary php compress html webpage code (clear spaces, line breaks, tabs, annotation mark, etc) php to generate excel and control 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.