Fix misplaced hyphens in regex
authorElliott Eggleston <ejegg@ejegg.com>
Mon, 5 Nov 2018 19:54:55 +0000 (14:54 -0500)
committerElliott Eggleston <ejegg@ejegg.com>
Mon, 5 Nov 2018 19:54:55 +0000 (14:54 -0500)
Inside a character range surrounded by [], the hyphen is interpreted
as defining a range, unless it's the first character. Move the
hyphens to the first character in the square brackets where it's
supposed to be interpreted as a hyphen.

Under PHP 7.3, the previous code failed with error message
preg_match(): Compilation failed: invalid range in character class at offset 7

CRM/Utils/Rule.php

index ee5661d4f35e078068cddb17518b6b30e8a53d6f..9b45124d25f903f087c04201fd1b5ed682cf0026 100644 (file)
@@ -108,7 +108,7 @@ class CRM_Utils_Rule {
     //   * Composed of alphanumeric chars, underscore and hyphens.
     //   * Maximum length of 64 chars.
     //   * Optionally surrounded by backticks, in which case spaces also OK.
-    if (!preg_match('/^((`[\w- ]{1,64}`|[\w-]{1,64})\.)?(`[\w- ]{1,64}`|[\w-]{1,64})$/i', $str)) {
+    if (!preg_match('/^((`[-\w ]{1,64}`|[-\w]{1,64})\.)?(`[-\w ]{1,64}`|[-\w]{1,64})$/i', $str)) {
       return FALSE;
     }