Need help finding and replacing text for "{#ANY_TEXT#}"

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
andrewlwong
Forum Newbie
Posts: 1
Joined: Tue Sep 15, 2009 9:53 am

Need help finding and replacing text for "{#ANY_TEXT#}"

Post by andrewlwong »

Hi,

I need your expert help. I'm trying to find a regular expression to replace all text between quotes "{#ANY_TEXT#}"

For example, if I have string like "This is a test {#ANY_TEXT#} string". I need to replace everything starting from {#...#} with another value. For example, If I want to make it blank, then end result should be "This is a test string". There could be several instances of this.

Thanks in advance for your help.

Andrew
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Need help finding and replacing text for "{#ANY_TEXT#}"

Post by prometheuzz »

Try this:

Code: Select all

$text = "This is a test {#ANY_TEXT#} string";
echo preg_replace('/{#(?:(?!#}).)*+#}/s', 'REPLACEMENT', $text);
Post Reply