Skip to content
This repository has been archived by the owner on Apr 12, 2023. It is now read-only.

Commit

Permalink
Switch regex to use nowdoc
Browse files Browse the repository at this point in the history
In certain environments (such as http://sandbox.onlinephpfunctions.com/),
`preg_match` complains if the pattern string contains a newline after
the pattern modifiers.  Using a nowdoc suppresses the end-of-string
newline.

```
Warning:  preg_match(): Unknown modifier '
'
```

No tests are being included with this fix because I could not reproduce
the error in the Docker or Travis CI environments.

Resolves #61
  • Loading branch information
lightster committed Mar 11, 2019
1 parent d8ab73f commit 4f5bbe8
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions src/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,39 @@ public function parse($format)
$parameter_map = [];
$replacement_sets = [];
$matches = [];
$pattern = '
@
(
(?:^|[^%])
(?:%%)*%
) # 1: the percent sign(s)
\( # literal open paren
([a-zA-Z_][a-zA-Z0-9_\-,:]*?) # 2: the name of the param
\) # literal close paren
$pattern = <<<'REGEX'
@
(
(?:^|[^%])
(?:%%)*%
) # 1: the percent sign(s)
\( # literal open paren
([a-zA-Z_][a-zA-Z0-9_\-,:]*?) # 2: the name of the param
\) # literal close paren
(
(?:
# standard sprintf format. reference http://php.net/sprintf
[+]? # an optional +/- sign specifier
(
(?:
# standard sprintf format. reference http://php.net/sprintf
[+]? # an optional +/- sign specifier
(?:[ 0]|\'.)? # an optional space/zero padding specifier or
# alternative padding character prefixed by a single quote
(?:[ 0]|\'.)? # an optional space/zero padding specifier or
# alternative padding character prefixed by a single quote
[\-]? # an optional alignment specifier
# ("-" for left-justified; right-justified otherwise)
[\-]? # an optional alignment specifier
# ("-" for left-justified; right-justified otherwise)
\d* # an optional width specifier
\d* # an optional width specifier
(?:\.(?:.?\d+)?)? # an optional precision specifier
# (a dot "." followed by an optional number, with an optional
# padding character in between the dot and the number)
(?:\.(?:.?\d+)?)? # an optional precision specifier
# (a dot "." followed by an optional number, with an optional
# padding character in between the dot and the number)
[bcdeEfFgGosuxX] # the type specifier
)?
[bcdeEfFgGosuxX] # the type specifier
)?
) # 3: the standard sprintf format
@mx
';
) # 3: the standard sprintf format
@mx
REGEX;
if (preg_match_all($pattern, $format, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
$percent_signs = $match[1];
Expand Down

0 comments on commit 4f5bbe8

Please sign in to comment.