如何从字符串中替换 {} [] () 大括号之间的所有内容? [英] How to replace everything between {} [] () braces from a string?
本文介绍了如何从字符串中替换 {} [] () 大括号之间的所有内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想删除大括号内的所有内容.例如,如果字符串是:
I want to remove everything inside braces. For example, if string is:
[hi] helloz [hello] (hi) {jhihi}
然后,我希望输出仅为 helloz
.
then, I want the output to be only helloz
.
我正在使用以下代码,但在我看来应该有更好的方法,是吗?
I am using the following code, however it seems to me that there should be a better way of doing it, is there?
$name = "[hi] helloz [hello] (hi) {jhihi}";
$new = preg_replace("/\([^)]+\)/","",$name);
$new = preg_replace('/\[.*\]/', '', $new);
$new = preg_replace('/\{.*\}/', '', $new);
echo $new;
推荐答案
这应该有效:
$name = "[hi] helloz [hello] (hi) {jhihi}";
echo preg_replace('/[\[{\(].*?[\]}\)]/' , '', $name);
将其粘贴到以下位置:http://writecodeonline.com/php/ 以查看它是否有效.
Paste it somewhere like: http://writecodeonline.com/php/ to see it work.
这篇关于如何从字符串中替换 {} [] () 大括号之间的所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文