Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php preg_split未在空间上拆分_Php_Regex_Preg Replace_Preg Split - Fatal编程技术网

Php preg_split未在空间上拆分

Php preg_split未在空间上拆分,php,regex,preg-replace,preg-split,Php,Regex,Preg Replace,Preg Split,我尝试使用preg_split在将字母或数字以外的任何内容替换为空格后,在任意数量的空格上拆分字符串。。。下面是我的代码(包括一些调试内容): 假设$data\u current[0]的值为“hello world”。我得到的结果是 hello world array ( [0] => hello world ) 显然,我希望数组有两个值“你好”和“世界” 这到底是怎么回事?$data\u current数组是从CSV中读取的(使用fgetcsv),如果这有帮助的话…问题是您使用

我尝试使用
preg_split
在将字母或数字以外的任何内容替换为空格后,在任意数量的空格上拆分字符串。。。下面是我的代码(包括一些调试内容):

假设
$data\u current[0]
的值为“hello world”。我得到的结果是

hello world
array
(
    [0] => hello world
)
显然,我希望数组有两个值“你好”和“世界”


这到底是怎么回事?
$data\u current
数组是从CSV中读取的(使用
fgetcsv
),如果这有帮助的话…

问题是您使用的是
PREG\u SPLIT\u NO\u EMPTY
但不是第四个参数,而是将其作为第三个参数,有效地设置了一个限制,请参见上的手册

你应使用:

preg_split('/\s+/', $input, -1, PREG_SPLIT_NO_EMPTY);
                                ^^ flags go in the 4th parameter of the function
                            ^^ default value, no limit
或:


问题在于,您使用的是
PREG\u SPLIT\u NO\u EMPTY
,但是您将其作为第三个参数,而不是第四个参数,有效地设置了一个限制,请参阅上的手册

你应使用:

preg_split('/\s+/', $input, -1, PREG_SPLIT_NO_EMPTY);
                                ^^ flags go in the 4th parameter of the function
                            ^^ default value, no limit
或:


若要拆分两个或多个空格,请更改

$array = preg_split('/[\s]+/', $input, PREG_SPLIT_NO_EMPTY);


若要拆分两个或多个空格,请更改

$array = preg_split('/[\s]+/', $input, PREG_SPLIT_NO_EMPTY);


使用$array=explode(“”,$input)怎么样?我想在多个空间上拆分。使用$array=explode(“”,$input)怎么样?我想在多个空间上拆分。
$array = preg_split('/[\s][\s]+/', $input);