0

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

Jocelyn
  • 11,209
  • 10
  • 43
  • 60
David Grabon
  • 107
  • 7
  • 2
    Can you give us an example, and show what code you have already tried? – Pitchinnate Mar 11 '13 at 14:21
  • Are you looking for words inside of a text or for characters inside some indiscriminate set of characters? – RandomWhiteTrash Mar 11 '13 at 14:23
  • I would say to go with preg_match() instead of a foreach loop. Add some code and we will help you out – aleation Mar 11 '13 at 14:26
  • David, if you solved your own question, post the solution in an Answer and accept it http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/ – CSᵠ Mar 11 '13 at 17:57
  • Won't let me, this is only the second time I've posted a question to StackOverflow, and I don't have enough reputation to answer my own question yet, says I need to wait 8 hours after the question was asked, so I figured I'd do it like this for now – David Grabon Mar 11 '13 at 18:34

1 Answers1

0

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

David Grabon
  • 107
  • 7