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.
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? Any help would be much appreciated
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)
$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]
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.