Thanks to Keith from the UK for pointing out something odd in my book that doesn’t seem to work as it did in earlier versions of PHP:
If you have a regular expression (I use the POSIX ones almost exclusively since they’re UTF-8 aware whereas the Perl ones were not when last I inquired), and you want to set a range for the number of matches on a particular expression you can use the syntax:
$expr = '[a-zA-Z]{5,50}'; // matches between 5 (incl) and 50 (incl) letters
Now, the problem is: what if you want to have the number of characters in the range be PHP variables that you can set in a configuration file or some such thing? Your first attempt, and what I used in my book, might be:
$expr = "[a-zA-Z]\{$min,$max}"; // double quotes for var expansion
And you would get a wonderfully annoying error message from the PHP engine:
Parse error: syntax error, unexpected ',', expecting '}' in Filename on line 5
No amount of backslashes will fix this problem. It turns out that the PHP parser consumes { and } characters when performing complex variable expansion, so …. all you have to do is add an extra set around each of the variables you wish to expand. PHP leaves the other two alone:
$expr = "[a-zA-Z]{{$min},{$max}}"; // extra { }s are consumed.
And what you are left with is a wonderfully working regular expression.