Php Strpos function is not working

I am doing text analysis. I have a table having positive words.The records are being fetched one by one and imploded in an array through mysqli_fetch_array.

while(($rowx = mysqli_fetch_array($resultx,MYSQLI_NUM)))
{

 $wordx = implode("", $rowx);

    if(strpos($text, $wordx) !== FALSE) 

    {

        $count1 = substr_count($text, $wordx);
        $pos_prob += .2 * $count1;
        echo "pos prob is".$pos_prob;
    }
}

But strpos is not able to match the string that is being fetched from the table.i.e. if text is ā€œItā€™s an excellent bookā€ the if condition is never true. Even though the word excellent is present in the table. And if I hard code the value $wordx as

$wordx='excellent';

Only then it works. Does anyone has any idea why this is happening? :frowning: Any help would be much appreciated :slight_smile:

When you retrieve the row from the table and implode it into $wordx, what would $wordx look like at that point?

strpos() does a full match of the needle, not a partial match. but your search string is a list of words and only if that search string as created (!) exists in the text, it will return true.

Itā€™s a simple positive word. I even tried echo ā€œstrpos($text, $wordx)ā€; to check the input but there was no problem in that.
And I get this output:
strpos(Its an excellent book, excellent)
strpos(Its an excellent book, excellently)

But when I hard code the value it shows results. If itā€™s doing full-match then that wouldnā€™t have been the case.

Therefore it must be something to do with assigning the value to $wordx. What happens if you try:

$wordx = $rowx[0];

instead of using implode()?

Also after youā€™ve assigned $wordx, can you var_dump both that and $text and show the results?

It gives exact same result as implode function

OK, and the var_dump() results?

Hereā€™s the output:
string ā€™ a+ā€™ (length=3)
array (size=1)
0 => string ā€™ a+ā€™ (length=3)
string ā€™ aboundā€™ (length=7)
array (size=1)
0 => string ā€™ aboundā€™ (length=7)
string ā€™ aboundsā€™ (length=8)
array (size=1)
0 => string ā€™ aboundsā€™ (length=8)
string ā€™ abundanceā€™ (length=10)
array (size=1)
0 => string ā€™ abundanceā€™ (length=10)

Results for $wordx and $text:
string ā€™ a+ā€™ (length=3)
string ā€˜Its an excellent bookā€™ (length=21)
string ā€™ aboundā€™ (length=7)
string ā€˜Its an excellent bookā€™ (length=21)
string ā€™ aboundsā€™ (length=8)
string ā€˜Its an excellent bookā€™ (length=21)
string ā€™ abundanceā€™ (length=10)
string ā€˜Its an excellent bookā€™ (length=21)
string ā€™ abundantā€™ (length=9)
string ā€˜Its an excellent bookā€™ (length=21)
string ā€™ accessableā€™ (length=11)
string ā€˜Its an excellent bookā€™ (length=21)
string ā€™ accessibleā€™ (length=11)
string ā€˜Its an excellent bookā€™ (length=21)

And can you show the section where one value of $wordx is present in the $text string?

Presumably the array you show in post #9 is the $rowx that comes from the query results?

string ā€˜Its an excellent bookā€™ (length=21)
string ā€™ excellentā€™ (length=10)
string ā€˜Its an excellent bookā€™ (length=21)
string ā€™ excellentlyā€™ (length=12)

I think this is what you were asking forā€¦

Try this:

$aWords = [
    ['Just testing to see if it works'], 
    ['It\'s an excellent book'], 
    ['Yet another test to see if it works'],
    ['This is an excellent book by an EXCELLENT author'], 
    ['Final test to see if it works'],
]; 
# while(($rowx = mysqli_fetch_array($resultx,MYSQLI_NUM)))
$pos_prob = 0; 
$text     = 'excellent';
$i2       = 0;
while ( $i2 < count($aWords) )
{
    $rowx  = $aWords[$i2]; 
    $sWords = implode("", $rowx );
    $sWords = strtolower( $sWords );

    # strpos( 'haystack', Needle);
    if( strpos($sWords, $text) !== FALSE) 
    {
      # substr_count( Haystack, Needls )
      $count1    = substr_count($sWords, $text);
      $pos_prob += .2 * $count1;
      echo "<br>pos prob is: ".$pos_prob;
      echo '<br>Found: <b>"' .$text .'"</b> in: "', $sWords .'"';
    }
  $i2++;
}


[quote] **Results:** pos prob is: 0.2 Found: "excellent" in: "it's an excellent book" pos prob is: 0.6 Found: "excellent" in: "this is an excellent book by an excellent author" [/quote]

Notice the Needle and Haystack.

Thanks a lot for the help. But even this works:

               $word1='excellent';
	   if(strpos($text, $wordx) !== FALSE) 
        {

            $count1 = substr_count($text, $wordx);
            $pos_prob += .2 * $count1;
            echo "pos prob is".$pos_prob;
        }

The problem is that the positive words from the database are not being matched.

I am amazed you can find a Haystack in a Needle!!!

Try this:

if(strpos($text, $wordx) !== FALSE) 
{
    $count1 = substr_count($text, $wordx);
    $pos_prob += .2 * $count1;
    echo "pos prob is".$pos_prob;
  
   // DEBUG
   echo '<br>Haystack => $text : ' .$text;
   echo '<br>Needle => $wordx : ' .$wordx;
   die;

}

No. $wordx is a needle here. And haystack is $text. I am trying to search needle in the haystack. And I believe this is how it works!

Why the two results?

The first result show excellent but the second result shows excellently which is not in the string"Its an excellent book"

OK, I was getting confused and thought you were trying to find a word in the database results instead of a searching for a string from the database results.

Because of the quotes around ā€œstrops( xx, xx)ā€ - OP is echoing what strings are being used rather than the result of the strpos() function.