regular expression: replace all text in between {$}

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

regular expression: replace all text in between {$}

Post by panic! »

Hello all,

A quick one:

I think it should be quite easy but I'm ignorant when it comes to learning regular expressions so here goes.

I'd like to (if it's possible) replace the text in a string like this:

<h2> {$text|title}</h2>
here is some copy

<img src="hello.jpg"/><br/>
{$html|caption}<br/>


with this:

<h2> %title%</h2>
here is some copy

<img src="hello.jpg"/><br/>
%caption%<br/>



so the format is

curly bracket then some text that needs to be erased then a pipe then some text that needs to be kept then a curly bracket.

{text to be erased|text to be kept}.

is that at all possible.

Thanks in advance for your help! :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: regular expression: replace all text in between {$}

Post by John Cartwright »

Untested

Code: Select all

preg_replace('#{[^|]+|([^}]+)}#, '%$1%', $input);
Post Reply