2017-11-16 19:41:29 2926次浏览 1条回答 1 悬赏 10 金钱

例如:国家(中国)(英国)
希望得到的是:中国 英国
注意:括号可能是中文的括号(),也可能是英文的括号()

补充于 2017-11-17 10:05
$str='国家(中国)(英国) ';
$rs='';
$str=str_replace(')', ')', str_replace('(', '(', $str));
preg_match_all("#\((.*?)\)#us",$str, $match_res);
foreach($match_res[1] as $item){
$rs.=$item.' ';
}
//得到的结果是:中国 英国

我的正则不好,如果有人会用正则直接搞定,不吝赐教

  • 回答于 2017-11-17 09:59 举报

    可以通过正则分割字符串来解决你的问题,代码如下

    $str = "(英文括号)(中文括号)";
    $arr = preg_split('/\(|\)\(|\)/', $str); 
    $array_filter = array_filter($arr); //去除括号左右的空格
    echo '<pre>';
    var_dump($array_filter);
    echo '</pre>';
    exit;
    
    2 条回复
    回复于 2017-11-17 10:25 回复

    这种方法不错,不过你写的有点问题,你可以自己测试下

    回复于 2017-11-17 10:44 回复

    测试结果:
    array(2) {
    [1]=>
    string(12) "英文括号"
    [2]=>
    string(12) "中文括号"
    }
    哪里出问题了呀

您需要登录后才可以回答。登录 | 立即注册
Steven0T
总监

Steven0T 上海

注册时间:2017-09-22
最后登录:2023-07-17
在线时长:28小时32分
  • 粉丝10
  • 金钱2970
  • 威望160
  • 积分4850

热门问题