Skip to content

Commit ccd5678

Browse files
committed
Fix regex dot/backslash issue (FakerPHP#206)
* fix regex backslash * fix regex backslash * fix regex backslash * run make cs
1 parent d6062dd commit ccd5678

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/Faker/Provider/Base.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -530,9 +530,21 @@ public static function regexify($regex = '')
530530
// replace \d with number and \w with letter and . with ascii
531531
$regex = preg_replace_callback('/\\\w/', 'static::randomLetter', $regex);
532532
$regex = preg_replace_callback('/\\\d/', 'static::randomDigit', $regex);
533-
$regex = preg_replace_callback('/(?<!\\\)\./', 'static::randomAscii', $regex);
534-
// remove remaining backslashes
533+
//replace . with ascii except backslash
534+
$regex = preg_replace_callback('/(?<!\\\)\./', static function () {
535+
$chr = static::asciify('*');
536+
537+
if ($chr === '\\') {
538+
$chr .= '\\';
539+
}
540+
541+
return $chr;
542+
}, $regex);
543+
// remove remaining single backslashes
544+
$regex = str_replace('\\\\', '[:escaped_backslash:]', $regex);
535545
$regex = str_replace('\\', '', $regex);
546+
$regex = str_replace('[:escaped_backslash:]', '\\', $regex);
547+
536548
// phew
537549
return $regex;
538550
}

test/Faker/Provider/BaseTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ public function regexifyDataProvider()
339339
['\.\*\?\+', 'escaped characters'],
340340
['[.]', 'literal dot in character class'],
341341
['.', 'catch-all dot'],
342+
['\\\\', 'escaped backslash'],
342343
['[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}', 'complex regex'],
343344
];
344345
}

0 commit comments

Comments
 (0)