Question

How can I find what characters are adjacent to a located string in php? I'm using a foreach loop to find each value inside a larger body of text with a variable of $value, so how would I also figure out what characters surround that string of $value? Sorry if this is confusing.

$count = 1;
foreach ($new_classes as $value)
    {
    $pageBody2 = $pageBody;
    $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);
        }
    }
echo $pageBody;

I want to find the characters around the string $value to see if they are alphanumeric or not (check if there are any characters that are alphabetic or numbers, excludes periods or whitespaces) so that if there are, it doesn't replace them

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top