Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

finding number in a string

Explorer ,
May 23, 2012 May 23, 2012

Copy link to clipboard

Copied

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?

TOPICS
Server side applications

Views

2.7K
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
May 23, 2012 May 23, 2012

Copy link to clipboard

Copied

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')

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 23, 2012 May 23, 2012

Copy link to clipboard

Copied

thanks for this, but first i have to check if the user exists first in the field.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
May 23, 2012 May 23, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 24, 2012 May 24, 2012

Copy link to clipboard

Copied

what I am trying to do is confirm a friend.  i have two columns in my db: pending and friends.  i have to move the pending id of the friend to confirm to friends.  first, i wanted to check if a user ID is actually in pending.  then, remove it from the string, and add that ID to the friends. 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 24, 2012 May 24, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 24, 2012 May 24, 2012

Copy link to clipboard

Copied

I understand that it starts at 0.  it should still work.  i had it working before.  i must've changed something. 

here is my code for passing to the function:

$query = mysql_query("SELECT friends, pendingFriendRequest

FROM members

WHERE username = '$userID'");

if (mysql_affected_rows() > 0) {

          $row = mysql_fetch_array($query) or die(mysql_error());

          $friends = $row['friends'];

          $pendingFriends = $row['pendingFriendRequest'];

}

// If the user exists in the waiting list, then

//continue (we must confirm that they are first by checking the DB

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

if($test){

          // If they are, then continue to add them

          $test = confirmFriend($pendingFriends, "25");

          if($test) {

                    echo "<br />You have confirmed " . findUserName(24) . "as a friend.";

          } else {

                    echo "<br />Error: Please try again, or contact us.";

          }

} else {

          echo "<br />Error: You either have this person as a friend already, or there was an internal error.";

}

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 24, 2012 May 24, 2012

Copy link to clipboard

Copied

>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)

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 24, 2012 May 24, 2012

Copy link to clipboard

Copied

ok.  but i was returning values "true" or "false", right?  and 0 evaluates to false?  or does it just take it as the number 0?

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 24, 2012 May 24, 2012

Copy link to clipboard

Copied

>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.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 24, 2012 May 24, 2012

Copy link to clipboard

Copied

Oh, so if it finds it or not, it automatically returns the value?   i can't overide it with "return true/false"?

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 24, 2012 May 24, 2012

Copy link to clipboard

Copied

>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.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 24, 2012 May 24, 2012

Copy link to clipboard

Copied

I see - thanks!

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 25, 2012 May 25, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 25, 2012 May 25, 2012

Copy link to clipboard

Copied

so maybe a friends table? 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 25, 2012 May 25, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 25, 2012 May 25, 2012

Copy link to clipboard

Copied

I was thinking about that.  it seems like you would have so many rows.  wouldn't it be slower, rather than just searching a string?

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 26, 2012 May 26, 2012

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines