I have modified it for a static array:
<?php
//$output = str_replace(' machinery safety', ' <b>machinery safety</b>', $modx->documentOutput);
//$modx->documentOutput = $output;
$output = $modx->documentOutput;
$rStart = strpos($output, '<!-- Keyword Replacement Start -->');
$rStop = strpos($output, '<!-- Keyword Replacement End -->');
$rLength = $rStop - $rStart;
if (($rStop !== false) && ($rStart !== false))
{
if (!function_exists('str_replace_once')) {
function str_replace_once($needle , $replace , $haystack){
// Looks for the first occurence of $needle in $haystack
// and replaces it with $replace.
$pos = strpos($haystack, $needle);
if ($pos === false) {
// Nothing found
return $haystack;
}
return substr_replace($haystack, $replace, $pos, strlen($needle));
}
}
$subStr = substr($output, $rStart, $rLength);
$rSubStr = $subStr ;
$keywords = "machinery safety,European Directive,CE Marking"; \\ ensure no spaces in before and after comma. Seperate keywords by commas
$array = explode(",", $keywords);
print_r($array);
foreach ($array as &$value) {
$replacementText = ' <a href="'.$value.'">'.$value.'</a>'; // might need to replace spaces with something for the href bit.
if (strpos($rSubStr, $replacementText ) === false)
$rSubStr = str_replace_once(' '.$value, $replacementText, $rSubStr);
}
$modx->documentOutput = str_replace($subStr, $rSubStr, $output);
//$modx->documentOutput = $tv;
}
?>
Hope this meets your requirements, I am still looking into the Template Variable route but not having much luck at the moment.