generate_operator_regex.php 1.0 KB

123456789101112131415161718192021222324252627
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. $operators = ['not', '!', 'or', '||', '&&', 'and', '|', '^', '&', '==', '===', '!=', '!==', '<', '>', '>=', '<=', 'not in', 'in', '..', '+', '-', '~', '*', '/', '%', 'matches', '**'];
  11. $operators = array_combine($operators, array_map('strlen', $operators));
  12. arsort($operators);
  13. $regex = [];
  14. foreach ($operators as $operator => $length) {
  15. // Collisions of character operators:
  16. // - an operator that begins with a character must have a space or a parenthesis before or starting at the beginning of a string
  17. // - an operator that ends with a character must be followed by a whitespace or a parenthesis
  18. $regex[] =
  19. (ctype_alpha($operator[0]) ? '(?<=^|[\s(])' : '')
  20. .preg_quote($operator, '/')
  21. .(ctype_alpha($operator[$length - 1]) ? '(?=[\s(])' : '');
  22. }
  23. echo '/'.implode('|', $regex).'/A';