Page 1 of 1

regular expression: replace all text in between {$}

Posted: Tue Apr 29, 2008 9:41 am
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! :)

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

Posted: Tue Apr 29, 2008 10:34 am
by John Cartwright
Untested

Code: Select all

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