简体   繁体   English

如何在PHP中使用正则表达式从简单括号中删除文本?

[英]How to remove text from simple parentheses using regex in PHP?

Problem: Text sample - "Wanted String (unwanted)" . 问题:文本示例- "Wanted String (unwanted)" There is only one string inside parentheses. 括号内只有一个字符串。

I need to remove the "(unwanted)" including parentheses using RegEx in PHP. 我需要在PHP中使用RegEx删除包括括号在内的"(unwanted)"

If you have only one pair of parentheses, you can use the following two regexes and then concatenate their result. 如果只有一对括号,则可以使用以下两个正则表达式,然后将其结果串联起来。 For eg, if your string is Wanted String (unwanted) and something after it , then: 例如,如果您的字符串是Wanted String (unwanted) and something after it ,则:

$result = preg_replace('/\(.*?\)/', '', $subject);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM