generate_operator_regex.php 1.1 KB

12345678910111213141516171819202122232425262728293031
  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. if ('cli' !== \PHP_SAPI) {
  11. throw new Exception('This script must be run from the command line.');
  12. }
  13. $operators = ['not', '!', 'or', '||', '&&', 'and', '|', '^', '&', '==', '===', '!=', '!==', '<', '>', '>=', '<=', 'not in', 'in', '..', '+', '-', '~', '*', '/', '%', 'matches', '**'];
  14. $operators = array_combine($operators, array_map('strlen', $operators));
  15. arsort($operators);
  16. $regex = [];
  17. foreach ($operators as $operator => $length) {
  18. // Collisions of character operators:
  19. // - an operator that begins with a character must have a space or a parenthesis before or starting at the beginning of a string
  20. // - an operator that ends with a character must be followed by a whitespace or a parenthesis
  21. $regex[] =
  22. (ctype_alpha($operator[0]) ? '(?<=^|[\s(])' : '')
  23. .preg_quote($operator, '/')
  24. .(ctype_alpha($operator[$length - 1]) ? '(?=[\s(])' : '');
  25. }
  26. echo '/'.implode('|', $regex).'/A';