【发布时间】:2010-10-27 19:59:08
【问题描述】:
我有一组字符串,我需要将它们分块成一个数组。
字符串需要在/、,、with 或& 上进行拆分。
不幸的是,一个字符串可能包含两个需要拆分的字符串,所以我不能使用split() 或explode()。
例如,一个字符串可以说first past/ going beyond & then turn,所以我试图获取一个可以返回的数组:
array('first past', 'going beyond', 'then turn')
我目前使用的代码是
$splittersArray=array('/', ',', ' with ','&');
foreach($splittersArray as $splitter){
if(strpos($string, $splitter)){
$splitString = split($splitter, $string);
foreach($splitString as $split){
我似乎无法在 PHP 中找到允许我执行此操作的函数。
我是否需要将字符串传递回漏斗顶部,并在字符串一次又一次拆分后继续通过foreach()?
这似乎效率不高。
【问题讨论】:
-
“不幸的是,一个字符串可能包含两个需要拆分的字符串,所以我不能使用拆分或分解。”你能解释一下你的意思吗?
-
我只是添加了一个例子来说明
标签: php arrays string preg-split csv