Skip to content

Commit

Permalink
Merge pull request #22 from mickadoo/bugfix/preg-replace-with-array-r…
Browse files Browse the repository at this point in the history
…eplacements

Handle validation when array of patterns is passed in
  • Loading branch information
jasny authored Aug 29, 2024
2 parents 0c1e2af + 57d1869 commit 95036e9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/PcreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,15 @@ public function getFilters()
*/
protected function assertNoEval($pattern)
{
$pos = strrpos($pattern, $pattern[0]);
$modifiers = substr($pattern, $pos + 1);
$patterns = (array)$pattern;

if (strpos($modifiers, 'e') !== false) {
throw new RuntimeError("Using the eval modifier for regular expressions is not allowed");
foreach ($patterns as $pattern) {
$pos = strrpos($pattern, $pattern[0]);
$modifiers = substr($pattern, $pos + 1);

if (strpos($modifiers, 'e') !== false) {
throw new RuntimeError("Using the eval modifier for regular expressions is not allowed");
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions tests/PcreExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ public function testReplaceWithArray()
);
}

public function testReplaceWithArrayOfPatterns()
{
$this->assertRender(
'0000AAAA',
"{{ '1234ABCD'|preg_replace(['/\\\\d/','/[A-Z]/'],['0', 'A']) }}"
);
}

public function testReplaceAssertNoEval()
{
$this->expectException(TwigRuntimeError::class);
Expand Down

0 comments on commit 95036e9

Please sign in to comment.