Page 1 of 1

preg_replace - help with regular expression

Posted: Mon Feb 12, 2007 12:02 pm
by php_dev
Hey guys,

I need help with a regular expression to replace a string passed to preg_replace, with the value in square brackets contained in the string.

E.g.

original string: 'foo = [bar]'

required string: 'bar'

Initially I can do:

$string = 'foo=[bar]';

echo preg_replace('%foo=(.*)%si', '\1', $string);

output:

[bar]

I've tried various expressions to ignore the square brackets, but nothings worked, so here I am!

Any ideas?

Thanks in advance.

Posted: Mon Feb 12, 2007 12:21 pm
by Luke
your question is very confusing... try and be more clear when asking questions. I can't understand what you're asking. Hope this helps:
viewtopic.php?p=357139

Re: preg_replace - help with regular expression

Posted: Mon Feb 12, 2007 1:15 pm
by blackbeard
I think this will work:

Code: Select all

$string = 'foo=[bar]';

echo preg_replace('%foo=\[(.*)\]%si', '\1', $string);

Posted: Tue Feb 13, 2007 3:23 am
by php_dev
Cheers Blackbeard, that's perfect.

Ninja Space Goat, sorry about that, I'll try and be much clearer in the future.