Skip to content

Commit 95036e9

Browse files
authored
Merge pull request #22 from mickadoo/bugfix/preg-replace-with-array-replacements
Handle validation when array of patterns is passed in
2 parents 0c1e2af + 57d1869 commit 95036e9

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/PcreExtension.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,15 @@ public function getFilters()
5959
*/
6060
protected function assertNoEval($pattern)
6161
{
62-
$pos = strrpos($pattern, $pattern[0]);
63-
$modifiers = substr($pattern, $pos + 1);
62+
$patterns = (array)$pattern;
6463

65-
if (strpos($modifiers, 'e') !== false) {
66-
throw new RuntimeError("Using the eval modifier for regular expressions is not allowed");
64+
foreach ($patterns as $pattern) {
65+
$pos = strrpos($pattern, $pattern[0]);
66+
$modifiers = substr($pattern, $pos + 1);
67+
68+
if (strpos($modifiers, 'e') !== false) {
69+
throw new RuntimeError("Using the eval modifier for regular expressions is not allowed");
70+
}
6771
}
6872
}
6973

tests/PcreExtensionTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ public function testReplaceWithArray()
116116
);
117117
}
118118

119+
public function testReplaceWithArrayOfPatterns()
120+
{
121+
$this->assertRender(
122+
'0000AAAA',
123+
"{{ '1234ABCD'|preg_replace(['/\\\\d/','/[A-Z]/'],['0', 'A']) }}"
124+
);
125+
}
126+
119127
public function testReplaceAssertNoEval()
120128
{
121129
$this->expectException(TwigRuntimeError::class);

0 commit comments

Comments
 (0)