|null */ public static function match( string $pattern, string $subject, bool $case_sensitive = false ): ?array { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- a broken pattern must never break the site. $result = @preg_match( self::compile( $pattern, $case_sensitive ), $subject, $groups ); return 1 === $result ? $groups : null; } /** * Replaces $1..$99 and ${1}..${99} in $target with captured groups (missing groups become ""). * * @param array $groups */ public static function substitute( string $target, array $groups ): string { return (string) preg_replace_callback( '/\$(?:\{(\d{1,2})\}|(\d{1,2}))/', static function ( array $m ) use ( $groups ) { if ( '' !== $m[1] ) { return $groups[ (int) $m[1] ] ?? ''; } // "$10" means group 10 only when it exists; otherwise group 1 followed by "0". if ( strlen( $m[2] ) === 2 && ! isset( $groups[ (int) $m[2] ] ) ) { return ( $groups[ (int) $m[2][0] ] ?? '' ) . $m[2][1]; } return $groups[ (int) $m[2] ] ?? ''; }, $target ); } public static function has_captures( string $target ): bool { return 1 === preg_match( '/\$(?:\{\d{1,2}\}|\d{1,2})/', $target ); } }