View Full Version : Some PHP-Regex Questions

10-07-2012, 09:15 PM
Below is a PHP Regex that someone helped me create...

if (preg_match_all('/\\{url=([a-zA-Z0-9-]+)\\}/', $body, $matches, PREG_SET_ORDER) !== false){

1.) Why are there double back-slashes before the left and right braces?

2.) How would I change this to say, "Match all instances where the vakue in between the Curly Braces is a Lower-Case Letter, Upper-Case Letter, Number, or Hyphen and is between 2 and 20 characters in length"??

3.) How would I modify this Regex to look for references to images in my "Article Body", where an Image could be either a PNG, JPEG, JPG or GIF?

Here is my best guess...

if (preg_match_all('/\\{img=([a-zA-Z0-9-]+)[.png|.jpeg|.jpg|.gif]\\}/', $body, $matches, PREG_SET_ORDER) !== false){



10-08-2012, 11:28 AM
1) backslashes are escape characters. if you want a literal backslash, you have to escape that one too

2) {2,20}

3) does your guess work?