php basics
PHP explode
The PHP explode function is used to split the string, and put each element of the cut into the PHP array for use in subsequent programs. If you don’t understand the complex regular expressions, you only need to simply split the string and take out some When used partially, the PHP explode function will be a very useful function.
Basic syntax of PHP explode function
explode (string $delimiter, string $string, int $limit)
Among them, string $delimiter starts cutting at a certain part of the string, which can also be said to be a cutting condition, string $string is the string to be cut, both items are required, and int $limit is Optional items, you can fill in positive or negative numbers. Filling in a positive number means that you can output several values at most, and the last value contains all the remaining parts of the cutting. If you fill in a negative number, the value of the negative number will not be displayed except for The other parts will show that we will prepare two examples to introduce separately.
Basic PHP explode example
<?php
$str ='ABCDEF G';
$str_sec = explode(" ",$str);
echo $str_sec[0].'-';
echo $str_sec[1].'-';
echo $str_sec[2 ].'-';
echo $str_sec[3].'-';
echo $str_sec[4].'-';
echo $str_sec[5].'-';
echo $str_sec[6];
?>
The above will output the result of "ABCDEFG". In the example, echo is used to return the explode to each part of the array. The individual output is only for easy understanding. It can also be changed to print_r to return the entire explode to the array result output at once, so it can be changed to the following This way of writing:$str ='ABCDEF G';
$str_sec = explode(" ",$str);
echo $str_sec[0].'-';
echo $str_sec[1].'-';
echo $str_sec[2 ].'-';
echo $str_sec[3].'-';
echo $str_sec[4].'-';
echo $str_sec[5].'-';
echo $str_sec[6];
?>
<?php
$str ='ABCDEF G';
$str_sec = explode(" ",$str);
print_r($str_sec);
?>
Changing the wording of print_r will output "Array ([0] => A [1] => B [2] => C [3] => D [4] => E [5] => F [6 ] => G) "Such an array represents the result.$str ='ABCDEF G';
$str_sec = explode(" ",$str);
print_r($str_sec);
?>
PHP explode example syntax plus the use of limit
<?php
$str ='ABCDEF G';
$str_sec = explode(" ",$str, 3 );
print_r($str_sec);
?>
The above will output the result "Array ([0] => A [1] => B [2] => CDEFG) ".$str ='ABCDEF G';
$str_sec = explode(" ",$str, 3 );
print_r($str_sec);
?>
Continuing the first example, we add the third parameter int $limit of the explode to the example. Here we use the number 3, which means that only three array elements are cut out, and the third array element contains For all the remaining parts of the string, you can also try to use negative values. Note that the int $limit parameter of PHP explode is a function only available after PHP 4.0.1, while the negative int $limit parameter It is a new feature added after PHP 5.1.0.
Post a Comment
0 Comments