开发者

php preg replace :: regex

开发者 https://www.devze.com 2023-01-21 22:22 出处:网络
If I have a string that looks like my name is {your name here} and I am from {country}. I am trying to use preg_replace to remove the {content} so the string ends up as my name isand I am from .

If I have a string that looks like my name is {your name here} and I am from {country}. I am trying to use preg_replace to remove the {content} so the string ends up as my name is and I am from .

But I can not work out the regex for the pattern.

Can someone please help me out?


my name is and I am from . is simply a sample st开发者_开发知识库ring.

it could be something like the current date is {current date} in {suburb}


Perhaps something like:

/\{[^{}]*}/

Untested, but should get you started. First you match the {, then everything except }, and then the final }.

edit: fixed it, and now it's actually tested ;)


You can try:

\{.*?}


For your "toolbox": there are online regular expression checkers, such as this one. It provides a good testbed since it is completely separate from your code.

+1 Evert -- you solution looks good.


$str = 'my name is {your name here} and I am from {country}.';
echo preg_replace('/{.*?}/', '', $str),"\n";

output:

my name is  and I am from .
0

精彩评论

暂无评论...
验证码 换一张
取 消