1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12$operators = ['not', '!', 'or', '||', '&&', 'and', '|', '^', '&', '==', '===', '!=', '!==', '<', '>', '>=', '<=', 'not in', 'in', '..', '+', '-', '~', '*', '/', '%', 'matches', '**'];
13$operators = array_combine($operators, array_map('strlen', $operators));
14arsort($operators);
15
16$regex = [];
17foreach ($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
27echo '/'.implode('|', $regex).'/A';
28