PHP 8.6 Deprecates mbstring Regex: Moving mb_ereg and mb_split to PCRE in Laravel

mb_ereg deprecated in PHP 8.6 with the whole mbstring regex family. The PCRE replacement for all 16 functions, the behaviour traps, and how to find them.

Steven Richardson
Steven Richardson
· 8 min read

A Laravel app moves to PHP 8.6 and the log fills up with the same line from a string helper nobody has opened in four years:

Deprecated: Function mb_ereg_replace() is deprecated since 8.6,
Oniguruma functions support ends PHP 9.0 in /app/Support/Slug.php on line 24

That notice is doing something unusual — it names the removal version. mb_ereg deprecated in 8.6 means gone in 9.0, so this is a migration with a deadline rather than a warning you can sit on. Here is the PCRE equivalent for every function in the family, and the three places where the swap is not like-for-like.

Confirm what PHP 8.6 deprecated and why#

The Oniguruma maintenance end RFC deprecates sixteen functions and one constant in 8.6, with removal scheduled for 9.0. Oniguruma — the regex engine behind every mb_* regex function — stopped being maintained on 24 April 2025, so this is a supply-chain decision, not a performance one. PHP cannot ship a security-sensitive C library that nobody upstream is patching.

The full list:

mb_ereg                  mb_ereg_search_getpos
mb_eregi                 mb_ereg_search_getregs
mb_ereg_match            mb_ereg_search_init
mb_ereg_replace          mb_ereg_search_pos
mb_eregi_replace         mb_ereg_search_regs
mb_ereg_replace_callback mb_ereg_search_setpos
mb_split                 mb_regex_encoding
mb_ereg_search           mb_regex_set_options

Plus the MB_ONIGURUMA_VERSION constant, and the mbstring.regex_retry_limit and mbstring.regex_stack_limit INI directives. If you wrote a version guard around MB_ONIGURUMA_VERSION to work around an old Oniguruma bug, that guard now emits its own notice — the constant is deprecated separately from the functions.

This is a bigger list than the PHP 8.5 round, and it lands in the same upgrade window as everything else in the PHP 8.6 upgrade checklist for Laravel. If you worked through the PHP 8.5 deprecations cheat sheet last year, the shape is familiar: notice now, fatal later.

Map each mb_ereg function to its PCRE equivalent#

Every deprecated function has a PCRE counterpart. The mapping is mechanical, but two rows need care — mb_ereg_match and the mb_ereg_search_* family — and they get their own steps below.

mbstring PCRE replacement
mb_ereg($p, $s, $m) preg_match("/$p/u", $s, $m)
mb_eregi($p, $s, $m) preg_match("/$p/iu", $s, $m)
mb_ereg_replace($p, $r, $s) preg_replace("/$p/u", $r, $s)
mb_eregi_replace($p, $r, $s) preg_replace("/$p/iu", $r, $s)
mb_ereg_replace_callback($p, $cb, $s) preg_replace_callback("/$p/u", $cb, $s)
mb_split($p, $s) preg_split("/$p/u", $s)
mb_ereg_match($p, $s) preg_match("/^$p/u", $s)
mb_regex_encoding() nothing — the /u modifier carries it
mb_regex_set_options() per-pattern modifiers
mb_ereg_search_* preg_match with PREG_OFFSET_CAPTURE

A slug helper is the most common place this shows up:

// Before — deprecated in 8.6
function slugify(string $title): string
{
    $slug = mb_ereg_replace('[^\w\s-]', '', mb_strtolower($title));

    return mb_ereg_replace('[\s_-]+', '-', trim($slug));
}

// After — PCRE, Unicode-aware
function slugify(string $title): string
{
    $slug = preg_replace('/[^\w\s-]/u', '', mb_strtolower($title));

    return preg_replace('/[\s_-]+/u', '-', trim($slug));
}

Note the delimiters. mbstring takes a bare pattern; PCRE does not. Every migrated pattern needs wrapping, and any / inside the pattern needs escaping or a different delimiter — #...#u is the usual escape hatch for anything matching paths or URLs.

Add the /u modifier to every migrated pattern#

Without /u, PCRE treats the subject as a sequence of bytes. . matches one byte, not one character; \w means ASCII word characters only; and a quantifier applied to a multibyte character will happily split it in half and hand you back invalid UTF-8. Oniguruma was multibyte-aware by default via mb_regex_encoding(), so a like-for-like swap that forgets /u is a silent data corruption bug, not a visible failure.

$name = 'Chloé Müller';

preg_match('/^\w+/', $name, $m);   // "Chlo"  — bytes
preg_match('/^\w+/u', $name, $m);  // "Chloé" — characters

One more trap: /u makes preg_* return false on invalid UTF-8 input rather than matching anything. That is usually what you want, but it means you now have to check the return value on data from file uploads and legacy database columns. preg_last_error_msg() tells you which it was.

Check where PCRE behaves differently from Oniguruma#

Three differences account for nearly every migration bug I have seen. POSIX bracket classes like [[:alpha:]] are Unicode-aware in Oniguruma but ASCII-only in PCRE unless /u is set — another reason the modifier is non-negotiable. Word boundaries differ too: \b in PCRE with /u uses Unicode properties, and patterns tuned against Oniguruma's boundary rules can match one character more or less than before.

The third is encoding scope. mb_regex_encoding('SJIS') set a global for every subsequent call. PCRE has no equivalent — /u means UTF-8 and nothing else. If your app genuinely regexes non-UTF-8 text, convert with mb_convert_encoding() first, or install the mb_onig PECL extension, which the RFC author wrote specifically as the bail-out for that case. For UTF-8 apps — which is almost all Laravel — you can delete the mb_regex_encoding() call outright.

The same multibyte-correctness thinking applies to the rest of your string layer; the mb_trim and mb_ucfirst additions in PHP 8.4 cover the non-regex half of it.

Rewrite the stateful mb_ereg_search loop#

The mb_ereg_search_* family is the only genuinely awkward migration, because it keeps a cursor in global state. mb_ereg_search_init() sets the subject and pattern, then each mb_ereg_search_pos() advances a hidden position. PCRE has no equivalent global, so you carry the offset yourself with PREG_OFFSET_CAPTURE.

// Before — stateful, deprecated
mb_ereg_search_init($text, '\d{4}-\d{2}-\d{2}');

$dates = [];
while ($pos = mb_ereg_search_pos()) {
    $dates[] = mb_substr($text, $pos[0], $pos[1]);
}
// After — explicit cursor
$dates = [];
$offset = 0;

while (preg_match('/\d{4}-\d{2}-\d{2}/u', $text, $m, PREG_OFFSET_CAPTURE, $offset)) {
    $dates[] = $m[0][0];
    // $m[0][1] is a BYTE offset, so advance by the match's byte length
    $offset = $m[0][1] + strlen($m[0][0]);
}

The comment matters. PREG_OFFSET_CAPTURE reports byte offsets even under /u, and the $offset argument to preg_match is also in bytes — so advance with strlen(), never mb_strlen(). Mixing the two is the single easiest way to turn this loop infinite on the first accented character.

If you only want every match and not the positions, skip the loop entirely: preg_match_all('/\d{4}-\d{2}-\d{2}/u', $text, $m) does the same job in one call.

Move Laravel string helpers to Str::of()#

Most of these call sites live in app/Support helpers that predate the fluent string API. Since Str sits on PCRE already, moving there kills the deprecation and the raw regex in the same edit.

use Illuminate\Support\Str;

// mb_split('\s+', $input)
$tokens = Str::of($input)->split('/\s+/u');

// mb_ereg_match('[A-Z]{2}\d{6}', $reference)
$valid = Str::of($reference)->isMatch('/^[A-Z]{2}\d{6}/u');

// mb_ereg_replace_callback('\{(\w+)\}', $fn, $template)
$rendered = Str::of($template)->replaceMatches('/\{(\w+)\}/u', $fn);

split() returns a Collection, not an array, which is usually an upgrade — but check the call site if the result goes straight into count() or a foreach with keys. replaceMatches() passes the full match array to the callback exactly as preg_replace_callback does, so callbacks port unchanged.

Find every call site before you upgrade#

No Rector set covers this migration yet — I checked, and the published rules for mbstring do not touch the regex family. The reliable tools are grep and PHPCompatibility, which already ships sniffs for all sixteen functions.

# Fast first pass
grep -rnE 'mb_ereg|mb_split|mb_regex_|MB_ONIGURUMA_VERSION' app/ src/ config/

# Thorough, and covers vendor packages you depend on
composer require --dev phpcompatibility/php-compatibility
vendor/bin/phpcs -p app/ --standard=PHPCompatibility --runtime-set testVersion 8.6-

Add phpstan/phpstan-deprecation-rules to the static analysis pass so new calls cannot creep back in — the same discipline that makes burning down a PHPStan baseline work. If your team already runs Pint, PHPStan and Rector behind one command, that is the place to wire it.

Do not reach for error_reporting(E_ALL & ~E_DEPRECATED). Silencing buys you until PHP 9.0 and then the call becomes a fatal Error in production with no warning window left.

Prove the migration with a multibyte Pest test#

A grep-and-swap migration needs evidence that the before and after agree. Pin the old behaviour as data — the expected output strings — and assert the new implementation produces them against a fixture with accents, CJK and a ZWJ emoji sequence.

it('slugifies multibyte titles identically to the mbstring version', function (string $input, string $expected) {
    expect(slugify($input))->toBe($expected);
})->with([
    ['Chloé Müller Writes', 'chloé-müller-writes'],
    ['日本語 の タイトル', '日本語-の-タイトル'],
    ['Team 👩‍💻 Ship It', 'team-👩‍💻-ship-it'],
]);

Run the suite with deprecations promoted to failures so a missed call site cannot hide. In phpunit.xml, set failOnDeprecation="true" and failOnPhpunitDeprecation="true" — any remaining mb_ereg* call then breaks CI rather than filling a log. Custom Pest expectations are worth a look if you end up asserting the same multibyte invariants across several helpers.

Wrapping Up#

Grep for the sixteen functions today, port the straightforward rows with /u on every pattern, and leave the mb_ereg_search_* loops for last — they are the only ones that need thought. Promote deprecations to failures in CI and the remaining call sites announce themselves.

The rest of the 8.6 surface is worth the same pass: the trim() form feed change is a quieter break in the same string layer, and the Laravel PHP 8.6 upgrade checklist sequences the whole move.

FAQ#

Why is mb_ereg deprecated in PHP 8.6?

Because Oniguruma, the regex engine the mbstring regex functions are built on, stopped being maintained on 24 April 2025. PHP cannot keep shipping a security-sensitive C library with no upstream patches, so the Oniguruma maintenance end RFC deprecated the whole family in 8.6 with removal in 9.0. It is a maintenance decision, not a performance or correctness one.

What replaces mb_split in PHP 8.6?

preg_split() with the /u modifier. mb_split('\s+', $text) becomes preg_split('/\s+/u', $text) — the pattern needs delimiters, which mbstring did not require, and /u is what makes PCRE treat the subject as UTF-8 characters rather than bytes. In Laravel you can also use Str::of($text)->split('/\s+/u'), which returns a Collection.

Does preg_match handle UTF-8 and multibyte strings?

Yes, but only with the u modifier on the pattern. Without it, PCRE operates byte by byte, so . matches a single byte and \w covers ASCII only — which quietly splits multibyte characters. With /u, PCRE uses Unicode semantics and returns false on invalid UTF-8 input, so check the return value on untrusted data.

Will mbstring regex functions be removed in PHP 9?

Yes. The RFC states removal in PHP 9.0 explicitly, and the 8.6 deprecation notice itself says Oniguruma function support ends in PHP 9.0. If you need them beyond that — typically for non-UTF-8 encodings such as Shift JIS — the RFC author maintains the mb_onig PECL extension as an external replacement.

How do I find deprecated mbstring regex calls in a Laravel codebase?

Start with grep -rnE 'mb_ereg|mb_split|mb_regex_|MB_ONIGURUMA_VERSION' app/ src/, then run PHP_CodeSniffer with the PHPCompatibility standard and --runtime-set testVersion 8.6- to catch calls in vendor packages too. Add phpstan/phpstan-deprecation-rules to static analysis and set failOnDeprecation="true" in phpunit.xml so the test suite surfaces anything the static pass misses. No Rector rule set covers this migration yet.

Steven Richardson
Steven Richardson

CTO at Digitonic. Writing about Laravel, architecture, and the craft of leading software teams from the west coast of Scotland.