Because your page fires this event 9 times. It might change if you added another chuck or snippet. It’s basically the number of times it calls this function and I believe it will vary from site to site - maybe even page to page if your templates differ sufficiently.
It will not always be 9 it wasn’t when it happened to me for instance.
[edit]
Also see last reply for something to try out.
[/edit]
[SECOND EDIT]
slight improvement - maybe just a readibility one but none the less it may help you understand what I have done a little more
<?php
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));
}
}
$output = $modx->documentOutput;
$replacementText = ' <a href="mahcinery-safety">machinery safety</a>';
if (strpos($output, $replacementText) === false)
{
$modx->documentOutput = str_replace_once(' machinery safety', $replacementText, $output);
} else {
return $modx->documentOutput;
}
?>
[/SECOND EDIT]