且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP的:从字符串中删除括号/内容?

更新时间:2023-02-23 09:29:25

这应该可以解决问题:

$str = trim(preg_replace('/\s*\([^)]*\)/', '', $str));

请注意,与其他建议不同,此答案也消除了括号内的空白.

Note, this answer removes whitespace around the bracket too, unlike the other suggestions.

修剪是为了防止字符串以方括号开头,在这种情况下,不会删除其后的空白.

The trim is in case the string starts with a bracketed section, in which case the whitespace following it isn't removed.