tags:

views:

75

answers:

3

Hi guys, I am trying to create a script that checks if a certain value is in a comma delimited string, and if the value is present, it should return true else it should return false.

Basically, I am trying to check if a user has voted for a specific person before, and if so, they cannot vote again, if they have not, vote and then add their uid to the database.

I am using explode to get all the values into an array, but I am unsure about the checking function, I have a few ideas but this is quite an important script so I wanted to see if there is a better way of going about it.

$test = "john,jack,tom";

$exploded = explode(",",$test);

$hostID = "john";

foreach($exploded as $one){
    if($one == $hostID){
        $return = TRUE;
    }else{
        $return = FALSE;
    };
};

Thanx in advance!

+3  A: 
  • You could use in_array.
  • Some people prefer to use lowercase true and false.
  • The semicolon after } is not needed
  • $return = TRUE does not actually stop the function. Maybe you do return $return.
  • Some people like to put spaces after comma's and keywords (e.g. foreach, if, else)
  • Some people prefer single quotes over double quotes

Code:

function find_name($name, $list)
{
    $exploded = explode(',', $list);
    return in_array($name, $exploded);
}

var_dump(find_name('john', 'john,jack,tom'));
?>
Sjoerd
Good advice. The code, as it stands, is flawed in that it will still set the value of `$return` to `false` if the `$hostID` is not the last element in the array (as any subsequent checks will change the value).
Lucanos
Thank you so much, that worked perfectly, and thanx for the tips!
Changing the boolean keywords to lowercase is a matter of personal preference. They are [case-insensitive](http://de2.php.net/manual/en/language.types.boolean.php). Personally, I find them easier to read in Uppercase and thus disagree to this particular advice.
Gordon
I agree with Gordon on that one, I also prefer having them uppercase, it's easier to read.
+1  A: 

You could also use

  • strpos — Find position of first occurrence of a string

instead:

 return strpos('john,jack,tom', 'barney'); // returns FALSE

Note that the function returns the position if the string was found, so searching for john will return zero, which would be FALSE if you compare it for equality (==). In other words use the identity comparator (===).

As Lucanos correctly points out, there is chance to get false positives when using strpos if the searched name is part of a longer name, e.g. searching for jean only would also find jean-luc. You could add a comma at the end of the haystack string and search for jean, though, but then again, it feels hackish. So it's likely better to use in_array.

Gordon
+1 for saving a lot of useless work if the string isn't there to begin with.
Tim Post
But `strpos()`, unless used as a preliminary check before doing an `in-array()` may return false positives. For instance `strpos( 'jean-paul,mark,steven' , 'jean' )` would return true.
Lucanos
@Lucanos that's true indeed. I'll add that to the answer
Gordon
+1  A: 

As Sjoerd says, in his very good advice.

function find_name( $needle , $haystack ) {
  if( strpos( $haystack , $needle )===false )
    return false;
  return in_array( $needle , explode( ',' , $haystack ) );
}

Test cases:

// find_name( 'john' , 'peter,mark,john' );
true
// find_name( 'peter' , 'mark,john' );
false

EDITED: As per advice from Gordon.

Lucanos