Preg_split and explode implement split textarea store content code

Source: Internet
Author: User
Today, there is an urgent bug, that is the background after configuring the whitelist, the mobile app is not valid, still display content. After receiving the mail, then went through the process, found that the background configuration whitelist is configured in the textarea, one line, and then in the code to see, using the Explode function segmentation, where the delimiter is \ r \ n. This article mainly share with you the method that PHP uses Preg_split and explode partition textarea to store content, combine the example form to analyze the function of preg_split and explode function, use skill and the related note in the process of text string segmentation.

The code is roughly as follows


Explode (' \ r \ n ', $val);

Then I tested it on my own development machine and found that it didn't split the contents of textarea into the database, so I found a useful function in the manual preg_split


$str = ' 12345 ';p rint_r (Preg_split ("/\n/", $str)),/*array (  [0] = 1  [1] = 2  [2] = 3  [3] = 4  [4] = 5) */

"Update"

In the afternoon by colleagues reminded, found that the original is a problem, because in Chrome and Firefox browser TextArea is a line break with \ n, and in IE is \ r \ n line break, so use str_replace replaced the next


$str = ' 12345 ';p rint_r (Explode ("\ n", Str_replace ("\ r \ n", "\ n", $str))); Array (  [0] = 1  [1] = 2  [2] = 3  [3] = 4  [4] = 5)

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.