generate_operator_regex.php 818 B

1234567891011121314151617181920212223
  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. // an operator that ends with a character must be followed by
  16. // a whitespace or a parenthesis
  17. $regex[] = preg_quote($operator, '/').(ctype_alpha($operator[$length - 1]) ? '(?=[\s(])' : '');
  18. }
  19. echo '/'.implode('|', $regex).'/A';