string $newHtmlUrl. * @return mixed * String, HTML. */ public static function replaceHrefUrls($html, $replace) { $useNoFollow = TRUE; $callback = function ($matches) use ($replace, $useNoFollow) { $replacement = $replace($matches[2]); // See: https://github.com/civicrm/civicrm-core/pull/12561 // If we track click-throughs on a link, then don't encourage search-engines to traverse them. // At a policy level, I'm not sure I completely agree, but this keeps things consistent. // You can tell if we're tracking a link because $replace() yields a diff URL. $noFollow = ''; if ($useNoFollow && $replacement !== $matches[2]) { $noFollow = " rel='nofollow'"; } return $matches[1] . $replacement . $matches[3] . $noFollow; }; // Find anything like href="..." or href='...' inside a tag. $tmp = preg_replace_callback( ';(\<[^>]*href *= *")([^">]+)(");', $callback, $html); return preg_replace_callback( ';(\<[^>]*href *= *\')([^\'>]+)(\');', $callback, $tmp); } // /** // * Find URL expressions; replace them with tracked URLs. // * // * @param string $msg // * @param int $mailing_id // * @param int|string $queue_id // * @param bool $html // * @return string // * Updated $msg // */ // public static function scanAndReplace_old($msg, $mailing_id, $queue_id, $html = FALSE) { // // $protos = '(https?|ftp)'; // $letters = '\w'; // $gunk = '/#~:.?+=&%@!\-'; // $punc = '.:?\-'; // $any = "{$letters}{$gunk}{$punc}"; // if ($html) { // $pattern = "{\\b(href=([\"'])?($protos:[$any]+?(?=[$punc]*[^$any]|$))([\"'])?)}im"; // } // else { // $pattern = "{\\b($protos:[$any]+?(?=[$punc]*[^$any]|$))}eim"; // } // // $trackURL = \CRM_Mailing_BAO_TrackableURL::getTrackerURL('\\1', $mailing_id, $queue_id); // $replacement = $html ? ("href=\"{$trackURL}\"") : ("\"{$trackURL}\""); // // $msg = preg_replace($pattern, $replacement, $msg); // if ($html) { // $msg = htmlentities($msg, ENT_NOQUOTES); // } // return $msg; // } }