views:

49

answers:

2

Hi All

I don't usually use regular expressions, hence my question. I need a regex to match the following:

'{any-string}'.

Any assistance appreciated.

+1  A: 

The most simple expression would be:

/{(.*?)}/

If you expect more complex strings (for example, some kind of escape sequence where the { and } characters are allowed within the string) it could be more complex. For instance, with a \ (backslash) escape sequence:

/{((?:\\.|[^}])*)}/

Edit: That's not tested, but the general idea is that the expression will swallow any character following the escape rather than ensuring it isn't the closing brace.

eyelidlessness
A: 

To replace anything in { } with what file_get_contents returns you can do:

$page = file_get_contents('some_file_name or some_url');
$str = '.....{...}....';
$str = preg_replace('/{[^}]*}/',"$page",$str);
codaddict