src/Control/HTTP.php
public static function urlRewriter($content, $code)
    {
        if (!is_callable($code)) {
            throw new InvalidArgumentException(
                'HTTP::urlRewriter expects a callable as the second parameter'
            );
        }

        // Replace attributes
        $attribs = ["src", "background", "a" => "href", "link" => "href", "base" => "href"];
        $regExps = [];
        foreach ($attribs as $tag => $attrib) {
            if (!is_numeric($tag)) {
                $tagPrefix = "$tag ";
            } else {
                $tagPrefix = "";
            }

            $regExps[] = "/(<{$tagPrefix}[^>]*$attrib *= *\")([^\"]*)(\")/i";
            $regExps[] = "/(<{$tagPrefix}[^>]*$attrib *= *')([^']*)(')/i";
            $regExps[] = "/(<{$tagPrefix}[^>]*$attrib *= *)([^\"' ]*)( )/i";
        }
        // Replace css styles
        // @todo - http://www.css3.info/preview/multiple-backgrounds/
        $styles = ['background-image', 'background', 'list-style-image', 'list-style', 'content'];
        foreach ($styles as $style) {
            $regExps[] = "/($style:[^;]*url *\\(\")([^\"]+)(\"\\))/i";
            $regExps[] = "/($style:[^;]*url *\\(')([^']+)('\\))/i";
            $regExps[] = "/($style:[^;]*url *\\()([^\"\\)')]+)(\\))/i";
        }

        // Callback for regexp replacement
        $callback = function ($matches) use ($code) {
            // Decode HTML attribute
            $URL = Convert::xml2raw($matches[2]);
            $rewritten = $code($URL);
            return $matches[1] . Convert::raw2xml($rewritten) . $matches[3];
        };

        // Execute each expression
        foreach ($regExps as $regExp) {
            $content = preg_replace_callback($regExp, $callback, $content);
        }

        return $content;
    }
public static function urlRewriter($content, $code)
    {
        if (!is_callable($code)) {
            throw new InvalidArgumentException(
                'HTTP::urlRewriter expects a callable as the second parameter'
            );
        }

        // Replace attributes
        $attribs = ["src", "background", "a" => "href", "link" => "href", "base" => "href"];
        $regExps = [];
        foreach ($attribs as $tag => $attrib) {
            if (!is_numeric($tag)) {
                $tagPrefix = "$tag ";
            } else {
                $tagPrefix = "";
            }

            $regExps[] = "/(<{$tagPrefix}[^>]*$attrib *= *\")([^\"]*)(\")/i";
            $regExps[] = "/(<{$tagPrefix}[^>]*$attrib *= *')([^']*)(')/i";
            $regExps[] = "/(<{$tagPrefix}[^>]*$attrib *= *)([^\"' ]*)( )/i";
        }
        // Replace css styles
        // @todo - http://www.css3.info/preview/multiple-backgrounds/
        $styles = ['background-image', 'background', 'list-style-image', 'list-style', 'content'];
        foreach ($styles as $style) {
            $regExps[] = "/($style:[^;]*url *\\(\")([^\"]+)(\"\\))/i";
            $regExps[] = "/($style:[^;]*url *\\(')([^']+)('\\))/i";
            $regExps[] = "/($style:[^;]*url *\\()([^\"\\)')]+)(\\))/i";
        }

        // Callback for regexp replacement
        $callback = function ($matches) use ($code) {
            // Decode HTML attribute
            $URL = Convert::xml2raw($matches[2]);
            $rewritten = $code($URL);
            return $matches[1] . Convert::raw2xml($rewritten) . $matches[3];
        };

        // Execute each expression
        foreach ($regExps as $regExp) {
            $content = preg_replace_callback($regExp, $callback, $content);
        }

        return $content;
    }
public static function urlRewriter ( $ content , $ code ) { if ( ! is_callable ( $ code ) ) { throw new InvalidArgumentException ( 'HTTP::urlRewriter expects a callable as the second parameter' ) ; } // Replace attributes $ attribs = [ "src" , "background" , "a" => "href" , "link" => "href" , "base" => "href" ] ; $ regExps = [ ] ; foreach ( $ attribs as $ tag => $ attrib ) { if ( ! is_numeric ( $ tag ) ) { $ tagPrefix = "$tag " ; } else { $ tagPrefix = "" ; } $ regExps [ ] = "/(<{$tagPrefix}[^>]*$attrib *= *\")([^\"]*)(\")/i" ; $ regExps [ ] = "/(<{$tagPrefix}[^>]*$attrib *= *')([^']*)(')/i" ; $ regExps [ ] = "/(<{$tagPrefix}[^>]*$attrib *= *)([^\"' ]*)( )/i" ; } // Replace css styles // @todo - http://www.css3.info/preview/multiple-backgrounds/ $ styles = [ 'background-image' , 'background' , 'list-style-image' , 'list-style' , 'content' ] ; foreach ( $ styles as $ style ) { $ regExps [ ] = "/($style:[^;]*url *\\(\")([^\"]+)(\"\\))/i" ; $ regExps [ ] = "/($style:[^;]*url *\\(')([^']+)('\\))/i" ; $ regExps [ ] = "/($style:[^;]*url *\\()([^\"\\)')]+)(\\))/i" ; } // Callback for regexp replacement $ callback = function ( $ matches ) use ( $ code ) { // Decode HTML attribute $ URL = Convert :: xml2raw ( $ matches [ 2 ] ) ; $ rewritten = $ code ( $ URL ) ; return $ matches [ 1 ] . Convert :: raw2xml ( $ rewritten ) . $ matches [ 3 ] ; } ; // Execute each expression foreach ( $ regExps as $ regExp ) { $ content = preg_replace_callback ( $ regExp , $ callback , $ content ) ; } return $ content ; }
Rewrite all the URLs in the given content, evaluating the given string as PHP code.

Put $URL where you want the URL to appear, however, you can't embed $URL in strings, for example:
<ul>
<li><code>'"../../" . $URL'</code></li>
<li><code>'myRewriter($URL)'</code></li>
<li><code>'(substr($URL, 0, 1)=="/") ? "../" . substr($URL, 1) : $URL'</code></li>
</ul>

As of 3.2 $code should be a callable which takes a single parameter and returns the rewritten,
for example:
<code>
function($url) {
return Director::absoluteURL($url, true);
}
</code>

@param string $content The HTML to search for links to rewrite.
@param callable $code Either a string that can evaluate to an expression to rewrite links
(depreciated), or a callable that takes a single parameter and returns the rewritten URL.

@return string The content with all links rewritten as per the logic specified in $code.
Rewrite all the URLs in the given content evaluating the given string as PHP code .