您的位置:首页 > 其它

类似explode, 但不分割指定符号内的字符.

2013-05-10 16:50 190 查看
/**
* 类似explode, 但不分割指定符号内的字符.
* @param string $str
* @param string $pattern 匹配指定符号内的正则
* @param string $replacement 暂时替换成的字符串
* @return array $data 分割之后的数组
*/
function str2arr($str, $pattern='/\((.*)\)/U', $replacement='()'){
$data = array();
$str = preg_replace('/\s+/', ' ', $str); //去多余空格
preg_match_all($pattern, $str, $matches); // print_r($matches);
$data = explode(' ', preg_replace($pattern, $replacement, $str));
foreach($data as $key=>$val){
if($val==$replacement){
$data[$key] = array_shift($matches[1]);
}
}
return $data;
}

$str = 'Html    Form    (Acl Auth) Help  component paginator collection (session cookie hash) result';
echo "<pre>";
print_r(str2arr($str));
/*
Array
(
[0] => Html
[1] => Form
[2] => Acl Auth
[3] => Help
[4] => component
[5] => paginator
[6] => collection
[7] => session cookie hash
[8] => result
)
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐