删除括号外的文字 [英] Remove the text outside the brackets

查看:64
本文介绍了删除括号外的文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$text = 'remove this text (keep this text and 123)';

echo preg_replace('', '', $text);

它应该输出:

(keep this text and 123)

推荐答案

将括号内的任何内容放入一个捕获组并仅保留它,如下所示:

Take anything found within the brackets, put it in a capture group and keep that only, like this:

echo preg_replace('/^.*(\(.*\)).*$/', '$1', $text);

这篇关于删除括号外的文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆