如何用PHP删除括号之间的文本

时间:2018-07-11 06:11:42

标签: php

如何删除方括号之间的文本。 对于exp。

$str = 'Aylmers(test, test2), Ancaster(Clandeboye, Bluevale)';

i want to get as
$str = 'Aylmers, Ancaster';

3 个答案:

答案 0 :(得分:4)

尝试一下:

apiVersion: rbac.authorization.k8s.io/v1beta1 
kind: ClusterRoleBinding 
metadata: 
    name: kubernetes-dashboard 
    labels: 
        k8s-app: kubernetes-dashboard
    namespace: kube-system
roleRef: 
  apiGroup: rbac.authorization.k8s.io 
  kind: ClusterRole 
  name: cluster-admin 
subjects: 
- kind: ServiceAccount 
  name: kubernetes-dashboard 

输出:

$str = 'Aylmers(test, test2), Ancaster(Clandeboye, Bluevale)';
echo preg_replace("/\([^)]+\)/","",$str );

Regex

答案 1 :(得分:0)

这应该做到:

`<?php`
`$string = "Stay 01 (Remove 01), Stay 02 (Remove 02)";`
`echo preg_replace("/\([^)]+\)/","",$string); // 'ABC '`

答案 2 :(得分:0)

如果还想删除括号中的嵌套数据。您可以使用:

$str = 'Aylmers(test, test2), Ancaster(Clandeboye, Bluevale)';
echo preg_replace("/\(([^()]*+|(?R))*\)/","", $str);

//output:
//Aylmers, Ancaster

说明:

  

/-分隔符

     

(-匹配的开头括号

     

[^)] +-匹配不是右括号的字符

     

)-匹配结束括号

     

/-结束分隔符