php basics
PHP string split and stored in array
There are three functions for splitting and storing PHP strings in the array, namely preg_split , mb_split and explode . These three functions are built-in functions of PHP and do not require additional installation. The usage is very simple, with a little parameter setting. Different, this article will introduce the three methods of dividing and storing PHP strings into arrays.
If you are not familiar with the usage of PHP arrays, please read the content of " PHP Array() Array Function Usage " first. If you are already familiar with the usage of arrays, please continue to read directly.
Basic syntax
PHP preg_split function (For details, please refer to: PHP preg_split function )
array preg_split (string $pattern, string $subject, int $limit, int $flags)
PHP mb_split function (see: PHP mb_split for details )array mb_split (regular expression of cutting rule, string to be processed, maximum number of splits);
PHP explode function (see: PHP explode for details )explode (string $delimiter, string $string, int $limit)
Practical example of PHP string split and stored in array<?php
echo'<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
$mystring="ABC";
print_r( mb_split("\s",$mystring ) ); //Cut
echo'<br>' according to the space ;
print_r( preg_split('//', $mystring, -1, PREG_SPLIT_NO_EMPTY) ); //Each English letter is cut
echo'<br>';
print_r ( explode(" ",$mystring) ); // Cut according to spaces
?>
Example outputecho'<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
$mystring="ABC";
print_r( mb_split("\s",$mystring ) ); //Cut
echo'<br>' according to the space ;
print_r( preg_split('//', $mystring, -1, PREG_SPLIT_NO_EMPTY) ); //Each English letter is cut
echo'<br>';
print_r ( explode(" ",$mystring) ); // Cut according to spaces
?>
Array ([0] => ABC)
Array ([0] => A [1] => B [2] => C)
Array ([0] => ABC)
In the example, we prepared an original string $mystring, the content is three English letters connected together, and then started to use mb_split function, preg_split function and explode function to split the string, because the first parameter of mb_split and explode All are set to cut according to spaces, so the final result is only one array value. Only the preg_split function will cut every English letter, but it does not mean that the preg_split function is the most powerful. It just happens that the parameter setting of the example is like this. , For the parameter settings of these three functions, please refer to the introduction in the following pages, you will find that they have more application changes.Array ([0] => A [1] => B [2] => C)
Array ([0] => ABC)
- PHP preg_split function
- PHP mb_split
- PHP explode
Post a Comment
0 Comments