类似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 ) */
相关文章推荐
- Split 获取字符串中指定符号分割的字符内容
- unicode编码字符保存时使用的分割符号技巧
- freemarker 将String 按照指定字符分割并生成 list 循环
- 实战c++中的string系列--string的分割、替换(类似string.split或是explode())
- 将字符串按照指定的长度进行分割(一个中文视为两个字符)
- 自己实现的一个字符串分割截取函数,以及查找指定字符
- delphi截取指定字符空格或-符号前面 中间 后面字符串
- 按照指定字符(@split )分割字符串,并取第@index 个
- 写一个函数,用来把一个字符串用指定的字符作为分隔符分割成若干个子串输出 | 写一个函数,用来返回一个字符串中重复出现的最长字串的长度及其开始地址
- 每隔指定字符长度分割字符串//银行卡号格式化
- php把字符串指定字符分割成数组
- SQL Server中自定义函数:用指定的分隔符号分割字符串
- C#下按照指定字符进行字符串分割
- C++字符串分割,分割符号可为任意字符,且可连续出现多个
- SQL 创建函数的样例(按指定的符号分割字符串,并返回临时表)
- php中将数组转换为指定符号分割的字符串
- [C语言]字符串处理 - 以指定的字符串分割字符串(支持中文字符)
- SQL Server中自定义函数:用指定的分隔符号分割字符串
- 字符串分割为字符串数组 去除前后指定字符
- Lua 用指定字符或字符串分割输入字符串,返回包含分割结果的数组