Are the spaces the same character in both $text and $wordx? I see you’ve got a space at the start of the string to search for when it comes out of the database, is it possible that it’s something other than a chr(32) in either string, hence the string not being found?
When you say it works if you hard-code the value of $wordx, you don’t put a space at the beginning of the string. Does that still work, if you put the space there?
OK, so I still wonder whether the two space characters are the same, whether what comes out of the database is somehow different to the space in $text and the space you type in when you hard-code the value of $wordx. You could try:
echo ord($wordx);
just after the implode() call at the start of your while() loop and see what result you get. That doesn’t seem to work for all encodings though, so it might not help.
As a test, you could also try trimming $wordx after you’ve created it - although that changes the functionality (in that it might also find the word “excellent” within “unexcellent”) it would be interesting to see if it helps - if it does, it points further to the leading space not being what it seems.
Yes, the problem is that what looks like a space at the front of each $wordx is actually a tab character, so when you use strpos() to find that in $text, it correctly does not find it because, in your example, the string does not contain “excellent”, only " excellent".
I am surprised that when you tried to trim($wordx) in post #26 it did not fix the problem, because that says it will remove the “\t” character at the start of the string. But you then need the space to make sure it only matches the start of each word.
Does this give any different result?
while(($rowx = mysqli_fetch_array($resultx,MYSQLI_NUM)))
{
$wordx = " " . trim(implode("", $rowx)); // lose the leading tab, but add a space
if(strpos($text, $wordx) !== FALSE)
{
$count1 = substr_count($text, $wordx);
$pos_prob += .2 * $count1;
echo "pos prob is".$pos_prob;
}
}
It sounds as if the database entries have a leading tab - were they imported from a tab-separated word list, for example? If the tab is in the database, I can’t think of why it should be.
To see what the ASCII value of the first character of the string was - for a space, it should be 20 (which is the hex of 32), so we can easily see that while it looks the same (because an ASCII tab is just displayed as a normal white-space by a browser) it actually isn’t.
@droopsnoot: Thanks a lot Genius It worked ! May God bless you. Actually I copied the values from an excel file and imported into database as csv. But now when I downloaded all values they do have a leading tab. This always happens with values when they are copied onto a text file. Now I’m trying to import all values without tab instead of removing it on run-time. Any suggestion on how to do it automatically? Because for checking whether leading tab is actual prob or not I manually entered 10 entries. And it worked. But I have thousands of values so I want some solution for this too…