要将给定的逗号分隔的字符串拆分为值数组,PHP代码如下-
<?php $my_string = "456,789,23, 4, 019"; $my_str_arr = preg_split ("/,/", $my_string); print_r("The array is "); print_r($my_str_arr); $my_string = "00, 876, 5432, 1234, 0"; $my_str_arr = explode (",", $my_string); print_r("The array is "); print_r($my_str_arr); ?>
输出结果
The array is Array ( [0] => 456 [1] => 789 [2] => 23 [3] => 4 [4] => 019 ) The array is Array ( [0] => 00 [1] => 876 [2] => 5432 [3] => 1234 [4] => 0 )
定义了一个数字字符串,并使用'preg_split'函数根据'/'或'。'的出现来分隔数字。现在将打印拆分的数组。以字符串形式拆分给定数字的另一种方法是使用爆炸函数。它根据逗号的出现拆分数组。