Jump to content

[SOLVED] help with a preg_replace expression


Renlok

Recommended Posts

well if i input a string which contains {TEST(0)} for example i wont it so the TEST is \\1 and the 0 is \\2 in the replace string ive tried this with this expression but it doesnt work and im not sure why

 

'#\{([a-z0-9\-_]+[\(0-9\)]+)\}#is'

 

thanks in advance for any help

One suggestion could be:

 

$str = '{TEST(0)}';
preg_match('#{([a-z0-9_-]+)\(([0-9]+)\)}#i', $str, $matches);
// $matches[1] a.k.a = TEST
// $matches[2] a.k.a = 0

Note that in your pattern, you can put the dash that you have in the first character class as first or last listed and thus won't need escaping. Also note some issues with your second half of the pattern. You are using a character class to look for either a ( [which inside a character class doesn't need escaping], or 0-9+ or a closing ). Note that all characters inside [..] only checks for a single character, not a sequence of characters.

 

EDIT - also note that the s modifier in your example won't do anything, as that only applies to the dot_match_all wildcard character in conjunction with it matching newlines.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.