preg_replace_callback

Supported Versions: PHP 4 >= 4.0.5, PHP 5, PHP 7, PHP 8
Perform a regular expression search and replace using a callback
<?php preg_replace_callback(    string|array $pattern,    callable $callback,    string|array $subject,    int $limit = -1,    int &$count = null,    int $flags = 0): string|array|null
<?php             return;

        }



        $value = preg_replace_callback('/([("]statamic:\/\/[^()"]*::)([^)"]*)([)"])/im', function ($matches) {

            return $matches[2] === $this->originalValue

                ? $matches[1].$this->newValue.$matches[3]

                : $matches[0];

        }, $value);



        if ($originalValue === $value) {

            return;
<?php             return $rule;

        }



        return preg_replace_callback('/{\s*([a-zA-Z0-9_\-]+)\s*}/', function ($match) {

            return Arr::get($this->replacements, $match[1], 'NULL');

        }, $rule);

    }



    public static function explodeRules($rules)
<?php     protected function resolveStatamicUrls(string $content)

    {

        return preg_replace_callback('/([("])statamic:\/\/([^()"]*)([)"])/im', function ($matches) {

            $data = Data::find($matches[2]);

            $url = $data ? $data->url() : '';



            return $matches[1].$url.$matches[3];

        }, $content);

    }

}
<?php     protected function versionJavascriptModules($contents)

    {

        return preg_replace_callback('~<script[^>]*type=("|\')module\1[^>]*>~i', function ($scriptMatches) {

            return preg_replace_callback('~src=("|\')(.*?)\1~i', function ($matches) {

                $quote = $matches[1];

                $url = $matches[2];



                $parameter = 't='.(microtime(true) * 10000);



                if (Str::contains($url, '?')) {

                    $url = str_replace('?', "?$parameter&", $url);

                } else {

                    $url .= "?$parameter";

                }



                return 'src='.$quote.$url.$quote;

            }, $scriptMatches[0]);

        }, $contents);

    }



    public static function contentType($type)
<?php     protected function versionJavascriptModules($contents)

    {

        return preg_replace_callback('~<script[^>]*type=("|\')module\1[^>]*>~i', function ($scriptMatches) {

            return preg_replace_callback('~src=("|\')(.*?)\1~i', function ($matches) {

                $quote = $matches[1];

                $url = $matches[2];



                $parameter = 't='.(microtime(true) * 10000);



                if (Str::contains($url, '?')) {

                    $url = str_replace('?', "?$parameter&", $url);

                } else {

                    $url .= "?$parameter";

                }



                return 'src='.$quote.$url.$quote;

            }, $scriptMatches[0]);

        }, $contents);

    }
<?php     {

        $domain = Site::current()->absoluteUrl();



        return preg_replace_callback('/="(\/[^"]+)"/ism', function ($item) use ($domain) {

            return '="'.Path::tidy($domain.$item[1]).'"';

        }, $value);

    }
<?php             return $route;

        }



        return preg_replace_callback('/{\s*([a-zA-Z0-9_\-]+)\s*}/', function ($match) {

            return "{{ {$match[1]} }}";

        }, $route);

    }



    private function routeData()
<?php         if (preg_match("/<\/(?:p|li|h1|h2|h3|h4|h5|h6|figcaption)>/ism", $value)) {


            $value = preg_replace_callback('/<.*?>/ism', function ($matches) {

                return str_replace(' ', '%###%##%', $matches[0]);

            }, $value);




            $value = preg_replace_callback("/(?<!<[p|li|h1|h2|h3|h4|h5|h6|div|figcaption])([^\s]\s)([^\s]*\s?){{$words}}(<\/(?:p|li|h1|h2|h3|h4|h5|h6|div|figcaption)>)/", function ($matches) {
<?php             }, $value);




            $value = preg_replace_callback("/(?<!<[p|li|h1|h2|h3|h4|h5|h6|div|figcaption])([^\s]\s)([^\s]*\s?){{$words}}(<\/(?:p|li|h1|h2|h3|h4|h5|h6|div|figcaption)>)/", function ($matches) {

                return preg_replace("/([\s])/", '&nbsp;', rtrim($matches[0]));

            }, $value);




            return str_replace('%###%##%', ' ', $value);
<?php         } else {

            return preg_replace_callback("/([^\s]\s)([^\s]*\s?){{$words}}$/im", function ($matches) {

                return preg_replace("/([\s])/", '&nbsp;', rtrim($matches[0]));

            }, $value);

        }

    }
<?php {

    public function wrap($text)

    {

        return preg_replace_callback('/:\w+/', function ($m) {

            return "<span class=\"notranslate\">{$m[0]}</span>";

        }, $text);

    }



    public function unwrap($text)
<?php     public function unwrap($text)

    {

        return preg_replace_callback('/<span class=\"notranslate\">(:\w+)<\/span>/', function ($m) {

            return $m[1];

        }, $text);

    }

}
<?php     protected function preg_replace_callback(...$args)

    {

        $return = preg_replace_callback(...$args);

        $this->handleRegexError();



        return $return;