php: regex remove bracket in string

前端 未结 4 2021
醉话见心
醉话见心 2021-01-22 05:16

similiar like this example, php: remove brackets/contents from a string? i have no idea to replace

$str = \'(ABC)some text\'

into



        
相关标签:
4条回答
  • 2021-01-22 05:50

    I'd avoid using regex altogether here. Instead, you could use normal string functions like this: $str = str_replace(array('(',')'),array(),$str);

    0 讨论(0)
  • 2021-01-22 05:54

    Instead of preg_replace, I would use preg_match:

    preg_match('#\(([^)]+)\)#', $str, $m);
    echo $m[1];
    
    0 讨论(0)
  • 2021-01-22 05:54

    Try this:

    $str = preg_replace('/\((.*?)\).*/','\\1',$str);
    
    0 讨论(0)
  • 2021-01-22 06:06

    If you want to use replace, you could use the following:

     $str = "(ABC)some text";
     $str = preg_replace("/^.*\(([^)]*)\).*$/", '$1', $str);
    

    The pattern will match the whole string, and replace it with whatever it found inside the parenthesis

    0 讨论(0)
提交回复
热议问题