public static function TransformInternalLinks($p_match)
{
static $internalLinkCounter = 0;
static $internalLinkStartTag = 0;
// This matches anchor links
$anchorStartRegex = "/<\\s*a\\s*(name\\s*=\\s*[\"']\\w+[\"'])+[\\s]*>/i";
if (preg_match($anchorStartRegex, $p_match[0])) {
// Leave the HTML tag as is
return $p_match[0];
}
// This matches '<a href="campsite_internal_link?IdPublication=1&..." ...>'
$internalLinkStartRegex = "/<\\s*a\\s*(((href\\s*=\\s*[\"'](\\/campsite\\/)?campsite_internal_link[?][\\w&=;]*[\"'])|(\\w+\\s*=\\s*['\"][_\\w]*['\"]))[\\s]*)*[\\s\\w\"']*>/i";
// This matches '</a>'
$internalLinkEndRegex = "/<\\s*\\/a\\s*>/i";
if (preg_match($internalLinkEndRegex, $p_match[0])) {
// Check if we are closing an internal link
if ($internalLinkCounter > 0) {
$internalLinkCounter = 0;
// Make sure the starting link was not blank (a blank
// indicates it was a link to no where)
if ($internalLinkStartTag != "") {
// Replace the HTML tag with a template tag
$retval = "<!** EndLink>";
$internalLinkStartTag = "";
return $retval;
} else {
// The starting link was blank, so we return blank for the
// ending link.
return "";
}
} else {
// Leave the HTML tag as is (for external links).
return '</a>';
}
} elseif (preg_match($internalLinkStartRegex, $p_match[0])) {
// Get the URL
preg_match("/href\\s*=\\s*[\"'](\\/campsite\\/)?(campsite_internal_link[?][\\w&=;]*)[\"']/i", $p_match[0], $url);
$url = isset($url[2]) ? $url[2] : '';
$parsedUrl = parse_url($url);
$parsedUrl = str_replace("&", "&", $parsedUrl);
$retval = "";
// It's possible that there isnt a query string - in which case
// its a link to no where, so we remove it ($retval is empty
// string).
if (isset($parsedUrl["query"])) {
// Get the target, if there is one
preg_match("/target\\s*=\\s*[\"']([_\\w]*)[\"']/i", $p_match[0], $target);
$target = isset($target[1]) ? $target[1] : null;
// Replace the HTML tag with a template tag
$retval = "<!** Link Internal " . $parsedUrl["query"];
if (!is_null($target)) {
$retval .= " TARGET " . $target;
}
$retval .= ">";
}
// Mark that we are now inside an internal link.
$internalLinkCounter = 1;
// Remember the starting link tag
$internalLinkStartTag = $retval;
return $retval;
}
}