Skip navigation
Currently Being Moderated

finding number in a string

May 23, 2012 4:42 PM

I am trying to find a number in a string from my database to replace it.  Here is my funciton:

 

function findUserID($friendsString, $userToFind){

          echo $friendsString . " " . $userToFind;

          $pos = strpos($friendsString,$userToFind);

          if($pos){

                    return true;

          }

          return false;

}

 

it returns "25,29 25".  How can it not find 25 in that string?

 
Replies
  • Currently Being Moderated
    May 23, 2012 5:00 PM   in reply to future-architect

    Is there are reason you are not doing this with just an UPDATE query? Something like the following:

     

    UPDATE 'table' SET 'column' = REPLACE('column', '25' 'replacementdata')

     

     
    |
    Mark as:
  • Currently Being Moderated
    May 23, 2012 7:03 PM   in reply to future-architect

    So it's not just replacing everywhere it encounters 25? At any rate, even if I don't quite understand what you are trying to do,  it still seems like the best way to do this is all in MySQL, not PHP.

     
    |
    Mark as:
  • Currently Being Moderated
    May 24, 2012 9:24 AM   in reply to future-architect

    Because string positions start at 0, not 1. Read the warning and solutions here: http://www.php.net/manual/en/function.strpos.php

     
    |
    Mark as:
  • Currently Being Moderated
    May 24, 2012 10:16 AM   in reply to future-architect

    >I understand that it starts at 0.  it should still work.

     

    No, it will not work.

     

    >$test = findUserID($pendingFriends, "25");

     

    If the position of the found string is 0, then $test equals 0

     

    >if($test){

     

    0 evaluates to boolean false, so this test fails, making it appear that the string was not found. Try

     

    if($test === false)

     
    |
    Mark as:
  • Currently Being Moderated
    May 24, 2012 10:28 AM   in reply to future-architect

    >but i was returning values "true" or "false", right?

     

    No. strpos() returns the position of the string if found, and FALSE if not found. If the string is found in position 0, then 0 is returned. Integer zero is evaluated as false. Hence the warning on the page I referred you to.

     
    |
    Mark as:
  • Currently Being Moderated
    May 24, 2012 10:48 AM   in reply to future-architect

    >Oh, so if it finds it or not, it automatically returns the value? 

     

    Let me try to explain again. The function strpos() returns two types of values. It returns an integer value if the string is found. It returns the boolean value FALSE if the string is not found. It never returns the boolean value TRUE. If the string is found in position zero, it returns the integer value zero. Zero is not a boolean value, but in php and many other languages, zero is evaluated as boolean FALSE.

     

    >i can't overide it with "return true/false"?

     

    I'm not sure what you mean. You have to treat the returned value in such as way that integer zero will not be evaluated as FALSE. Use the "===" operator for this. Or in the case of replacing the code you have, use the "!==" operator

     

    if($test !== false)

     

    If strpos() did not find the string, $test will equal false and the expression above will evaluate false.  If it returns any integer, the expression will evaluate as true.

     
    |
    Mark as:
  • Currently Being Moderated
    May 25, 2012 11:43 AM   in reply to future-architect

    You're welcome. It appears to me that the reason you've run into this problem stems from the fact that you are storing multiple values in a single column.

     

    >i have two columns in my db: pending and friends.

     

    This creates all sorts of problems and violates the most basic normalization rules. Instead, think about moving that relation into another table.

     
    |
    Mark as:
  • Currently Being Moderated
    May 25, 2012 4:34 PM   in reply to future-architect

    Yes. It could have columns for the user, friend and a boolean to indicate if friend is confirmed or pending.

     
    |
    Mark as:
  • Currently Being Moderated
    May 26, 2012 3:04 PM   in reply to future-architect

    No, it won't be slower. How many rows are you anticipating? Put an index on the column that will be used in the where clause.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points