Contao\Controller::convertRelativeUrls PHP Method

convertRelativeUrls() public static method

Convert relative URLs in href and src attributes to absolute URLs
public static convertRelativeUrls ( string $strContent, string $strBase = '', boolean $blnHrefOnly = false ) : string
$strContent string The text with the URLs to be converted
$strBase string An optional base URL
$blnHrefOnly boolean If true, only href attributes will be converted
return string The text with the replaced URLs
    public static function convertRelativeUrls($strContent, $strBase = '', $blnHrefOnly = false)
    {
        if ($strBase == '') {
            $strBase = \Environment::get('base');
        }
        $search = $blnHrefOnly ? 'href' : 'href|src';
        $arrUrls = preg_split('/((' . $search . ')="([^"]+)")/i', $strContent, -1, PREG_SPLIT_DELIM_CAPTURE);
        $strContent = '';
        for ($i = 0, $c = count($arrUrls); $i < $c; $i = $i + 4) {
            $strContent .= $arrUrls[$i];
            if (!isset($arrUrls[$i + 2])) {
                continue;
            }
            $strAttribute = $arrUrls[$i + 2];
            $strUrl = $arrUrls[$i + 3];
            if (!preg_match('@^(?:[a-z0-9]+:|#)@i', $strUrl)) {
                $strUrl = $strBase . ($strUrl != '/' ? $strUrl : '');
            }
            $strContent .= $strAttribute . '="' . $strUrl . '"';
        }
        return $strContent;
    }