You are here

protected function MetaNameBase::parseImageUrl in Metatag 8

Extract any image URLs that might be found in a meta tag.

Return value

string A comma separated list of any image URLs found in the meta tag's value, or the original string if no images were identified.

1 call to MetaNameBase::parseImageUrl()
MetaNameBase::output in src/Plugin/metatag/Tag/MetaNameBase.php
Generate the HTML tag output for a meta tag.

File

src/Plugin/metatag/Tag/MetaNameBase.php, line 401

Class

MetaNameBase
Each meta tag will extend this base.

Namespace

Drupal\metatag\Plugin\metatag\Tag

Code

protected function parseImageUrl($value) {
  global $base_root;

  // If image tag src is relative (starts with /), convert to an absolute
  // link; ignore protocol-relative URLs.
  if (strpos($value, '<img src="/') !== FALSE && strpos($value, '<img src="//') === FALSE) {
    $value = str_replace('<img src="/', '<img src="' . $base_root . '/', $value);
  }
  if ($this
    ->multiple()) {

    // Split the string into an array, remove empty items.
    $values = array_filter(explode(',', $value));
  }
  else {
    $values = [
      $value,
    ];
  }

  // Check through the value(s) to see if there are any image tags.
  foreach ($values as $key => $val) {
    $matches = [];
    preg_match('/src="([^"]*)"/', $val, $matches);
    if (!empty($matches[1])) {
      $values[$key] = $matches[1];
    }
    else {
      $values[$key] = PlainTextOutput::renderFromHtml($val);
    }
  }

  // Make sure there aren't any blank items in the array.
  $values = array_filter($values);

  // Convert the array back into a comma-delimited string before sending it
  // back.
  return implode(',', $values);
}