PHP mb_split

 The PHP mb_split function uses regular expressions to split a string of multiple bytes. Unlike PHP str_split , mb_split can cut where you want to split, and process it according to the regular expression instructions, but there must be a bit of regular expression Although the concept of formula can handle more delicate results when used, the relative complexity is also higher.


Basic syntax of PHP mb_split

array mb_split (regular expression of cutting rule, string to be processed, maximum number of splits);


The first parameter "Regular expression of the cutting rule" and the second parameter "The string to be processed" are required items, telling PHP which string of characters you plan to use in what way, the third parameter" "Maximum cutting quantity" tells PHP that only a few elements can be cut at most. This is an optional item.

Basic PHP mb_split example
<?php
 $String = "Good morning to you.";
 $Arr = mb_split("\s",$String);
 print_r($Arr);
?>

In the example, the regular expression of mb_split is written as "\s", which means that the string is cut according to spaces, so the following result will be output:



If we add the limit of the third parameter "Maximum number of cuts", rewrite the code
<?php
 $String = "Good morning to you.";
 $Arr = mb_split("\s",$String,3);
 print_r($Arr);
?>
The output result will be such that the



array elements have changed from the original four to only three, and the third element is all the remaining strings after cutting out the first two elements. Since the PHP mb_split function involves some basic concepts of PHP regular expressions, it will be slightly more difficult to use. There is another function that can handle similar results. Please refer to the PHP explode function.

Post a Comment

0 Comments