Skip to content

Stdlib: preg_replace_callback_array() — multi-pattern callback replace (ext/pcre/php_pcre.c parity) #3568

Description

@PurHur

Category

stdlib (PCRE) | php-src-strict for enum operands

Problem

preg_replace_callback() exists (#1177), but the array-of-patterns variant is missing.

$out = preg_replace_callback_array(
    ['/\d+/' => fn($m) => '['.$m[0].']'],
    'a1b2'
);
echo $out;

Today: Call to undefined function preg_replace_callback_array().

Zend PHP 8.2: prints a[1]b[2].

Once implemented, enum-case subject operands must TypeError (not LogicException / silent backing coerce) per ext/pcre/php_pcre.c — same php-src-strict policy as #7153 / #7154.

php-src reference

Repro — missing function

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php bin/vm.php -r "echo function_exists(\"preg_replace_callback_array\") ? \"yes\" : \"no\";"'
# today: no

Save as test/repro-maintainer/preg_replace_callback_array.php:

<?php
$out = preg_replace_callback_array(
    ['/\d+/' => fn(array $m): string => '[' . $m[0] . ']'],
    'a1b2'
);
echo $out, "\n";

Repro — enum subject (after basic impl; php-src-strict)

<?php
enum E: string { case A = 'x'; }
try {
    preg_replace_callback_array(['/x/' => fn($m) => 'y'], E::A);
} catch (Throwable $e) {
    echo get_class($e), ': ', $e->getMessage(), "\n";
}

Zend: TypeError naming E. VM today: undefined function (later: must not LogicException).

Scope

Path Work
ext/standard/preg_replace_callback_array.php VM builtin — reuse VmPregReplaceCallback / VmPreg internals
ext/standard/VmPreg.php subject guard via VmString::coerceStringBuiltinArg() + enum case rejection
ext/standard/Module.php register
ext/standard/JitPregReplaceCallback.php JIT follow-up optional v1
Tests test/compliance/cases/stdlib/preg_replace_callback_array.phpt
Tests test/compliance/cases/stdlib/preg_replace_callback_array_enum.phpt (php-src-strict)

Done when

  • Basic repro prints a[1]b[2] on VM
  • Mixed pattern keys (string/int) and subject array semantics match Zend preg_replace_callback_array tests
  • Enum-case subject → TypeError with enum class name (php-src-strict)
  • ./script/ci-fast.sh --filter preg_replace_callback_array green
  • Capability matrix row added (VM min; JIT follow-up optional)

Notes

Build on existing preg_replace_callback() VM/JIT infrastructure in ext/standard/VmPreg*.php. Pairs #6639 (JIT wiring) and #5999 (enum batch for other preg_*).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:vmVirtual machineenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-4:stdlibPhase 4 – stdlib for web apps

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions