PHP uses Preg_split and explode to implement the method of partitioning textarea storage content

Source: Internet
Author: User
Tags explode php explode
This article mainly introduces the method that PHP uses Preg_split and explode partition textarea to store content, and analyzes the functions of preg_split and explode functions, the use of techniques and the related considerations in the text string segmentation process with the example form. A friend you need can refer to the following

Today, there is an urgent bug, that is the background after configuring the whitelist, the mobile app is not valid, still display content. Received the message, 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, 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)

Related recommendations:

PHP explode function Introduction and how to use a detailed explanation

2017 php explode function definition and Usage summary

PHP Split () string segmentation function Application Example

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.