You are here

function _absolute_url in Relative Path to Absolute URLs 7

Same name and namespace in other branches
  1. 6 rel_to_abs.module \_absolute_url()

Absolute url callback.

Parameters

string $txt: Text to be parsed.

string $base_url: Base url of the site to prefix.

Return value

string Processed text.

1 call to _absolute_url()
rel_to_abs_filter_html in ./rel_to_abs.module
Calback for filter.

File

./rel_to_abs.module, line 50
Relative Paths to Absolute URLs

Code

function _absolute_url($txt, $base_url) {
  $needles = array(
    'href="',
    'src="',
    'background="',
  );
  $new_txt = '';
  if (substr($base_url, -1) != '/') {
    $base_url .= '/';
  }
  $new_base_url = $base_url;
  $base_url_parts = parse_url($base_url);
  foreach ($needles as $needle) {
    while ($pos = strpos($txt, $needle)) {
      $pos += strlen($needle);
      if (substr($txt, $pos, 7) != 'http://' && substr($txt, $pos, 8) != 'https://' && substr($txt, $pos, 6) != 'ftp://' && substr($txt, $pos, 7) != 'mailto:' && substr($txt, $pos, 2) != '//' && substr($txt, $pos, 1) != '#') {
        $new_txt .= substr($txt, 0, $pos) . $new_base_url;
      }
      else {
        $new_txt .= substr($txt, 0, $pos);
      }
      $txt = substr($txt, $pos);
    }
    $txt = $new_txt . $txt;
    $new_txt = '';
  }
  return $txt;
}