请考虑以下事项:
$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
我做错了什么?
答案 0 :(得分:11)
str_replace不能使用正则表达式,而是使用preg_replace:
答案 1 :(得分:2)
您需要使用preg_replace
代替:
$string = "A string with {LABELS} and {more|232} {lbls} and some other stuff";
echo preg_replace( '/\{.*?\}/', '', $string );
答案 2 :(得分:0)
尝试:
echo preg_replace('/\{.*?\}/', '', $string);
答案 3 :(得分:0)
preg_replace('/\{.*?\}/','',$str)
答案 4 :(得分:0)
一定要使用preg_replace,但还你需要一个稍微不同的正则表达式来过滤空格并确保你正确匹配花括号
$string = "A string with {LABELS} and {more|232} {lbls} and some other stuff";
echo preg_replace('/\s*\{[^}]*\}/', '', $string);
给:带有和其他东西的字符串