You are here

public function RelToAbs::absoluteUrl in Relative Path to Absolute URLs 8

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 RelToAbs::absoluteUrl()
RelToAbs::process in src/Plugin/Filter/RelToAbs.php
Performs the filter processing.

File

src/Plugin/Filter/RelToAbs.php, line 47

Class

RelToAbs
Provides a filter to convert relative paths to absolute URLs.

Namespace

Drupal\rel_to_abs\Plugin\Filter

Code

public function absoluteUrl($txt, $base_url) {
  $needles = array(
    'href="',
    'src="',
    'background="',
  );
  $new_txt = '';
  if (substr($base_url, -1) != '/') {
    $base_url .= '/';
  }
  $new_base_url = $base_url;

  // Check if Drupal installed in subdirectory.
  $sub_dir = FALSE;
  $parts = parse_url($new_base_url);
  if ($parts['path'] !== '/') {
    $sub_dir = $parts['path'];
  }
  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) != '#' && substr($txt, $pos, 4) != 'tel:') {
        $new_txt .= substr($txt, 0, $pos) . $new_base_url;
      }
      else {
        $new_txt .= substr($txt, 0, $pos);
      }
      $txt = substr($txt, $pos);

      // Remove leading sub-directory prefix if site installed in subdir.
      if ($sub_dir) {
        if (substr($txt, 0, strlen($sub_dir)) == $sub_dir) {
          $txt = substr($txt, strlen($sub_dir));
        }
      }
    }
    $txt = $new_txt . $txt;
    $new_txt = '';
  }
  return $txt;
}