【发布时间】:2015-03-10 12:48:26
【问题描述】:
我正在尝试使用preg_split 在任意数量的空格上拆分字符串,在用空格替换字母或数字以外的任何内容之后...这是我的代码(包括一些调试内容):
$input = strtolower($data_current[0]);
$input = preg_replace('/[^a-z0-9]/', ' ', $input);
echo($input."\r\n");
$array = preg_split('/[\s]+/', $input, PREG_SPLIT_NO_EMPTY);
print_r($array);
die;
假设$data_current[0] 的值为'hello world'。我得到的输出是这样的......
hello world
array
(
[0] => hello world
)
显然,我期待一个包含两个值的数组...“hello”和“world”。
这里到底发生了什么?如果有帮助,则从 CSV(使用 fgetcsv)中读取 $data_current 数组...
【问题讨论】:
-
使用 $array =explode(' ',$input) 怎么样?
-
我想分割多个空格。
标签: php regex preg-replace preg-split