You are here

function _print_mail_encode_urls in Printer, email and PDF versions 5.4

Same name and namespace in other branches
  1. 5.x print_mail/print_mail.inc \_print_mail_encode_urls()

Callback function for the preg_replace_callback replacing spaces with %20

Replace spaces in URLs with %20

Parameters

$matches: array with the matched tag patterns, usually <a...>+text+</a>

Return value

tag with re-written URL

1 string reference to '_print_mail_encode_urls'
print_mail_form_submit in print_mail/print_mail.inc
Process the send by-email form submission.

File

print_mail/print_mail.inc, line 343

Code

function _print_mail_encode_urls($matches) {

  // first, split the html into the different tag attributes
  $pattern = '!\\s*(\\w+\\s*=\\s*"(?:\\\\"|[^"])*")\\s*|\\s*(\\w+\\s*=\\s*\'(?:\\\\\'|[^\'])*\')\\s*|\\s*(\\w+\\s*=\\s*\\w+)\\s*|\\s+!';
  $attribs = preg_split($pattern, $matches[1], -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
  foreach ($attribs as $key => $value) {
    $attribs[$key] = preg_replace('!(\\w)\\s*=\\s*(.*)!', '$1=$2', $value);
  }
  $size = count($attribs);
  for ($i = 1; $i < $size; $i++) {

    // If the attribute is href or src, we may need to rewrite the URL in the value
    if (preg_match('!^(?:href|src)\\s*?=(.*)!i', $attribs[$i], $urls) > 0) {
      $url = trim($urls[1], " \t\n\r\0\v\"'");
      $new_url = str_replace(' ', '%20', $url);
      $matches[1] = str_replace($url, $new_url, $matches[1]);
    }
  }
  $ret = '<' . $matches[1] . '>';
  if (count($matches) == 4) {
    $ret .= $matches[2] . $matches[3];
  }
  return $ret;
}