我正在尝试清除" "
,,
,.
,?
,!
字符串,但结果数组包含空元素。< / p>
我的代码:
$mesaj = "Ana are mere, dar nu are si nuci? Matei are nuci!";
$keywords = preg_split("/[\s,.?!]+/", $mesaj);
print_r($keywords);
输出如下:
Array (
[0] => Ana
[1] => are
[2] => mere
[3] => dar
[4] => nu
[5] => are
[6] => si
[7] => nuci
[8] => Matei
[9] => are
[10] => nuci
[11] =>
)
我想从上面的数组中删除空元素。怎么办呢?
答案 0 :(得分:3)
使用PREG_SPLIT_NO_EMPTY
标志:
$keywords = preg_split("/[\s,.?!]+/", $mesaj, -1, PREG_SPLIT_NO_EMPTY);
上述语句中的-1
用于启用标志。有关详细信息,请参阅documentation for preg_split()
。