preg_replace正则表达式标记未被替换

Hoping you can help. Pretty new to regex and although I have written this regex it doesnt seem to match. I dont recieve an error message so im assuming the syntax is correct but its just not being applied?

I want the regex to replace content like

{foo}bar{/foo} with bar

Here is my code:

$regex = "#([{].*?[}])(.*?)([{]/.*?[}])#e";
$return = preg_replace($regex,"('$2')",$return);

Hope someone can help. Not sure why it doesnt seem to work.

Thanks for reading.

Your regex does work, however it isn't smart enough to know that the end tag has to be the same as the start tag. I would use this instead. I've also simplified it a little:

$regex = '#{([^}]*)}(.*?)\{/\\1}#';

echo preg_replace('{foo}bar{/foo}', '$2', $str); // outputs "bar"

Codepad

Refering to my comment above:

#(?:[{](.*?)[}])(.*?)(?:[{]/\1[}])#

uses a backreference to keep the tags equal. Also, I used non-capture parentheses to keep the useless groups out: $1 will be the tag name, and $2 will be the tag content.

Note that you will have to apply the replacement several times if your tags can nest.