php:正则表达式删除字符串中的括号 [英] php: regex remove bracket in string
本文介绍了php:正则表达式删除字符串中的括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
类似于这个例子,php:从字符串中删除括号/内容?我不知道要替换
similiar like this example, php: remove brackets/contents from a string? i have no idea to replace
$str = '(ABC)some text'
进入
$str = 'ABC';
当前使用 $str = preg_replace('/(.)/','',$str);
但不起作用.如何解决这个问题?
currently use $str = preg_replace('/(.)/','',$str);
but not works. how to fix this?
推荐答案
如果你想使用替换,你可以使用以下:
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
这篇关于php:正则表达式删除字符串中的括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文