PHP uses preg_split and explode to implement the method of splitting textarea to store content

墨辰丷
Release: 2023-03-27 06:24:02
Original
1574 people have browsed it

This article mainly introduces the method of PHP using preg_split and explode to split textarea to store content. It combines the examples to analyze the functions of preg_split and explode functions, usage skills and related precautions in the text string splitting process. Friends in need can For reference

There is an urgent bug today. It is said that after configuring the whitelist in the background, the mobile app is invalid and the content is still displayed. After receiving the email, I went through the process and found that the background configuration whitelist was configured in the textarea, one per line. Looking at the code again, I found that the explode function was used to separate it, and the separator was \r\n. The code is roughly as follows

explode('\r\n', $val);
Copy after login

After that, I tested it on my development machine and found that this would not split the content of the textarea stored in the database at all. , so I searched in the manual and found a very useful function preg_split

$str = '1
2
3
4
5';
print_r(preg_split("/\n/",$str));
/*
Array
(
  [0] => 1
  [1] => 2
  [2] => 3
  [3] => 4
  [4] => 5
)
*/
Copy after login

[update]

I was reminded by a colleague in the afternoon, It turned out that there was a problem with the separator, because in Chrome and Firefox browsers, textarea has a newline character of \n, while in IE, it has a newline character of \r\n, so I used str_replace to replace

$str = '1
2
3
4
5';
print_r(explode("\n", str_replace("\r\n", "\n", $str)));
Array
(
  [0] => 1
  [1] => 2
  [2] => 3
  [3] => 4
  [4] => 5
)
Copy after login

Related recommendations:

php explode function introduction and usage Detailed explanation

##2017php explode Summary of function definition and usage

##php split() string split function application example

The above is the detailed content of PHP uses preg_split and explode to implement the method of splitting textarea to store content. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!