preg_replace - help with regular expression

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
php_dev
Forum Newbie
Posts: 3
Joined: Mon Feb 12, 2007 11:41 am

preg_replace - help with regular expression

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Re: preg_replace - help with regular expression

Post by blackbeard »

I think this will work:

Code: Select all

$string = 'foo=[bar]';

echo preg_replace('%foo=\[(.*)\]%si', '\1', $string);
php_dev
Forum Newbie
Posts: 3
Joined: Mon Feb 12, 2007 11:41 am

Post by php_dev »

Cheers Blackbeard, that's perfect.

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