Php Strpos function is not working

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?

Yes. It still works with

$word1=' excellent';

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.

Actually, try this instead:

echo bin2hex($wordx);

just after you’ve run implode() on the query result, and show the results.

Try this ans substitue the correct values in the appropriate position:

$errorLog = 'zzz-errorLog.php';
ini_set('error_log', $errorLog);
    error_log(__LINE__. ' Just Testing');
    error_log(__LINE__. ' Just Testing');
    error_log(__LINE__. ' Just Testing');
highlight_file( $errorLog );

I trimmed. But nothing happened. :worried:

Which values?

OK, show us what bin2hex() shows for $wordx.

Few values:
0961626f756e64
0961626f756e6473
096162756e64616e6365
096162756e64616e74
0961636365737361626c65
0961636365737369626c65
096163636c61696d
096163636c61696d6564
096163636c616d6174696f6e
096163636f6c616465
096163636f6c61646573

You want me to send hex values for all records in the database? And what is the use btw?

you hava a TAB before each word.

2 Likes

What should I do then? Because all I am doing is:

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

When I output $wordx with this code:

 echo '<br>'.$wordx;

I get this:

a+
abound
abounds
abundance
abundant
accessable
accessible
acclaim
acclaimed
acclamation
accolade
accolades
accommodative
accomodative
accomplish
accomplished
accomplishment
accomplishments
accurate
accurately
achievable
achievement
achievements
achievible
acumen
.
.
.

it depends on how you saved the needles in the DB. if the tab is present in the DB, you can remove it.

Try str_replace(“\t”, “”, $wordx);

I am surfing on a tablet so cannot try the script.

Never mind, misread the post.

To remove leading whitespace, just trim the needle before searching for it.

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.

1 Like

@droopsnoot: Thanks a lot Genius :slight_smile: It worked :dizzy: ! 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…

@Dormilich : Thank you :slight_smile: That was the problem.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.