SOLVED: I figured out what to do:
$count = 1;
foreach ($new_classes as $value)
{
$pos = stripos($pageBody, $value);
$left = $pageBody[$pos - 1];
$right = $pageBody[$pos + strlen($value)];
if (!ctype_alpha($left) && (!ctype_alpha($right) || ($right == 's')))
{
$pageBody2 = $pageBody;
if ($right == 's')
{
$pageBody = str_replace($value . 's', '<a id="link" name="links" href="http://thecompendium.comuv.com/Compendium/index.html?page=' . $value . '">' . $value . 's' . '</a>', $pageBody, $count);
if ($pageBody == $pageBody2)
{
$pageBody = str_replace(strtolower($value) . 's', '<a id="link" name="links" href="http://thecompendium.comuv.com/Compendium/index.html?page=' . $value . '">' . strtolower($value) . 's' . '</a>', $pageBody, $count);
}
}
else
{
$pageBody = str_replace($value, '<a id="link" name="links" href="http://thecompendium.comuv.com/Compendium/index.html?page=' . $value . '">' . $value . '</a>', $pageBody, $count);
if ($pageBody == $pageBody2)
{
$pageBody = str_replace(strtolower($value), '<a id="link" name="links" href="http://thecompendium.comuv.com/Compendium/index.html?page=' . $value . '">' . strtolower($value) . '</a>', $pageBody, $count);
}
}
}
}
Someone else posted a code that defined the $left, $right, and $pos variables (however they deleted their answer a bit later sadly and I was unable to get their name to give them credit). I adapted that into an if statement to create the links to the words if they were surrounded by a whitespace, period, or ending in an s.
WRITTEN EXPLANATION OF SOLUTION: I located the position of $value in $pageBody using stripos for case insensitive location (because the word may have a different case than its name as a class). Then using that position, and I defined the character to the left of the word as $left by pulling the position of $value in $pageBody and going down 1, then defined the character to the right of the word as $right by pulling the position again, and going up by the number of characters in $value. I ran a validation on $left to make sure that it was not a letter, and make sure $right was either the letter s, or not a letter at all. If it passed the validation test then I ran the remainder of the code. However if either character to the side of $value is a letter (excluding s as $right) then it doesn't execute the code