Search code examples
phptwiliostrpos

php strpos always returning false


I'm attempting to determine if a user sent a text message containing a certain string.

I'm having a strange issue with strpos always returning false, unless the needle parameter of the strpos function is hardcoded as a string, i.e. "Sam". I'm fetching the value for the desired needle variable via mysqli_fetchassoc, and comparing it to the text message body retrieved from $_REQUEST['Body'].

while ($row = mysqli_fetch_assoc($queryResult))
    {           
        if (mysqli_num_rows($queryResult) > 1)
        {
            //$_REQUEST['Body'] is the body of the incoming text message that Twilio will process
            $messageBody= $_REQUEST['Body'];
            $recName = $row['rFName'];
            $recId = $row['recipientID'];



            if (strpos($messageBody, $recName) !== false)
            ...

If I hardcode the needle parameter as a string, like below, then it works fine.

 //This works
           if (strpos($messageBody, "Sam") !== false)
            ...

I've done a type check on both of the comparison values and ensured they are strings. I must be missing something simple here.


Solution

  • I figured it out.

    Strpos pays attention to whitespace, go figure. In my scenario, I had a space at the end of my test string, that looked like this: Sam.(An added space at the end of Sam) Be sure to check for any stray whitespace in your string if you have this issue.