1 | <?php declare(strict_types = 1); |
||
13 | class Pattern |
||
14 | { |
||
15 | /** |
||
16 | * Don't forget to close parentheses when using $define |
||
17 | */ |
||
18 | const DEFINE = "/(?(DEFINE)"; |
||
|
|||
19 | |||
20 | # Definitions of sub patterns for a valid interval |
||
21 | const INTEGER = <<<'REGEX' |
||
22 | (?<integer> |
||
23 | (?:\G|(?!\n)) |
||
24 | (\s*\b)? |
||
25 | \d{1,5} |
||
26 | \s* |
||
27 | ) |
||
28 | REGEX; |
||
29 | |||
30 | # Starts with integer followed by time specified |
||
31 | const TIME_PART = <<<'REGEX' |
||
32 | (?<timepart> |
||
33 | (?&integer) |
||
34 | ( s(ec(ond)?s?)? |
||
35 | | m(on(ths?)?|in(ute)?s?)? |
||
36 | | h(rs?|ours?)? |
||
37 | | d(ays?)? |
||
38 | | w(eeks?)? |
||
39 | ) |
||
40 | ) |
||
41 | REGEX; |
||
42 | |||
43 | # Regex to match a valid interval, holds the value in $matches['interval'] |
||
44 | const INTERVAL_ONLY = "^(?<interval>(?&timepart)++)$/uix"; |
||
45 | } |
||
46 |
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.