分解函数分隔符空格


<?php
$str = '1000 - 2000';
$str = preg_replace('/s+/', '', $str);
// zero limit
print_r(explode('-',$str,0));

?>  

http://ideone.com/rFvgZI

我试图获得两个数组项目"1000"和"2000"无济于事。我在这里做错了什么?

删除第三个参数以分解。将第三个参数设置为 0,您基本上会得到一个返回的包含整个字符串的元素数组......

PARAMETERS
· $delimiter
- The boundary string.
· $string
- The input string.
· $limit
-  If  $limit  is  set and positive, the returned array will contain a
   maximum of $limit elements with the last element containing the rest 
   of $string.  If the $limit parameter is negative, all components except 
   the last -$limit are returned.  If the $limit parameter is zero, then 
   this is treated as 1.

那(对于所有空格)呢:

<?php
    $str = '1000 - 2000';
    $tmp = explode('-', preg_replace('/s+/', '', $str));
    var_dump($tmp);
?>
这样做

就可以了

$arr = preg_split('/s*-s*/', $str);

这是将给定的字符串拆分为(零个或多个空格 (\s*) + 一个连字符 + 零个或多个空格 (\s*))。

最新更新