You are here

public function SpamspanTrait::callbackMailto in SpamSpan filter 8.2

Same name and namespace in other branches
  1. 8 src/SpamspanTrait.php \Drupal\spamspan\SpamspanTrait::callbackMailto()

Callback function for preg_replace_callback.

Replaces mailto <a> tags.

Parameters

array $matches: The matches from preg_match. $matches[0] - the entire matched string. $matches[1] - any attributes before href attribute. $matches[2] - the matched quote for href attribute. $matches[3] - the entire mailto href attribute value. $matches[4] - the local part of the email address. $matches[5] - the domain of the email address. $matches[6] - any attributes after href attribute. $matches[7] - the contents of <a> tag.

Return value

string The replaced text.

1 call to SpamspanTrait::callbackMailto()
SpamspanTrait::replaceMailtoLinks in src/SpamspanTrait.php

File

src/SpamspanTrait.php, line 95

Class

SpamspanTrait
Trait SpamspanTrait.

Namespace

Drupal\spamspan

Code

public function callbackMailto(array $matches) {

  // Take the mailto: URL in $matches[3] and split the query string
  // into its component parts, putting them in $headers as
  // [0]=>"header=contents" etc.  We cannot use parse_str because
  // the query string might contain dots.
  // Single quote can be encoded as &#039; which breaks parse_url.
  // Replace it back to a single quote which is perfectly valid.
  $matches[3] = str_replace("&#039;", '\'', $matches[3]);
  $query = parse_url($matches[3], PHP_URL_QUERY);
  $query = str_replace('&amp;', '&', $query);
  $headers = preg_split('/[&;]/', $query);

  // If no matches, $headers[0] will be set to '' so $headers must be reset.
  if ($headers[0] == '') {
    $headers = [];
  }

  // Take all <a> attributes except the href and put them into custom $vars.
  $vars = $attributes = [];

  // Before href.
  if (!empty($matches[1])) {
    $matches[1] = trim($matches[1]);
    $attributes[] = $matches[1];
  }

  // After href.
  if (!empty($matches[6])) {
    $matches[6] = trim($matches[6]);
    $attributes[] = $matches[6];
  }
  if (count($attributes)) {
    $vars['extra_attributes'] = implode(' ', $attributes);
  }
  return $this
    ->output($matches[4], $matches[5], $matches[7], $headers, $vars);
}