Php 如何从字符串中删除标签?

Php 如何从字符串中删除标签?,php,regex,replace,Php,Regex,Replace,考虑以下几点: $string = "A string with {LABELS} and {more|232} {lbls} and some other stuff"; echo str_replace('/(\{.*?\})/', '', $string); 我正在尝试删除所有标签(标签是{方括号}之间的任何文本)。预期产出为: A string with and and some other stuff 但我得到的是原始字符串: A string with {LABELS} and

考虑以下几点:

$string = "A string with {LABELS} and {more|232} {lbls} and some other stuff";
echo str_replace('/(\{.*?\})/', '', $string);
我正在尝试删除所有标签(标签是
{方括号}
之间的任何文本)。预期产出为:

A string with and and some other stuff
但我得到的是原始字符串:

A string with {LABELS} and {more|232} {lbls} and some other stuff

我做错了什么?

str\u replace不适用于正则表达式,请使用preg\u replace:


您需要使用
preg\u replace

$string = "A string with {LABELS} and {more|232} {lbls} and some other stuff";
echo preg_replace( '/\{.*?\}/', '', $string );
尝试:


请确保使用preg_replace,但也需要一个稍微不同的正则表达式来过滤掉空格,并确保正确匹配花括号

$string = "A string with {LABELS} and {more|232} {lbls} and some other stuff";
echo preg_replace('/\s*\{[^}]*\}/', '', $string);

给出:一个带and和其他内容的字符串

你问题中的两个字符串是相同的!?!?!您的预期结果和实际结果看起来相同。我看不到预期结果和原始结果之间的差异。您是对的,我的错:)请查看编辑。这将匹配字符串中第一个
{
和最后一个
}
之间的所有内容。
preg_replace('/\{.*?\}/','',$str)
$string = "A string with {LABELS} and {more|232} {lbls} and some other stuff";
echo preg_replace('/\s*\{[^}]*\}/', '', $string);