0

考虑以下:

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

我正在尝试删除所有标签(标签是 之间的任何文本{brackets})。预期的输出是:

A string with and and some other stuff

但我得到的是原始字符串:

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

我究竟做错了什么?

4

5 回答 5

11

str_replace 不适用于正则表达式,请改用 preg_replace:

http://php.net/manual/en/function.preg-replace.php

于 2012-04-17T15:28:49.793 回答
2

您将需要preg_replace改用:

$string = "A string with {LABELS} and {more|232} {lbls} and some other stuff";
echo preg_replace( '/\{.*?\}/', '', $string );
于 2012-04-17T15:29:29.590 回答
0

尝试:

echo preg_replace('/\{.*?\}/', '', $string);
于 2012-04-17T15:29:33.927 回答
0
preg_replace('/\{.*?\}/','',$str)
于 2012-04-17T15:33:13.317 回答
0

请务必使用 preg_replace,但您需要稍微不同的正则表达式来过滤掉空格并确保正确匹配大括号

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

Gives:一个带有 and 和其他东西的字符串

于 2012-04-17T15:35:32.847 回答