Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems comparing strings

Status
Not open for further replies.

groovygarden

Programmer
Aug 23, 2001
63
Hi, Hope someone can help or point me in the direction of help....!

I'm trying to compare two strings. When I echo the strings they are exactly the same (or at least they appear exactly the same, yet when I compare them using

Code:
$list[$i]==$selected
or
Code:
strcmp($selected, $list[$i])

...I don't get a match. $list is an array of names created from a mysql select statement (spaces are encoded with  ). $selected is a value the user has selected from a listbox. Does this have something to do with it?

Cases match fine and there are no leading/trailing spaces and I've tried encoding the spaces in $selected as well. Nothing seems to work!

Any help on this one much appreciated as I'm pretty much stuck if I can't compare these two strings!

Thanks a lot
 
try using a perl regular expression like preg_match_all()
and use it to loop through your array

Sometimes, when my code just won't behave, I take it outside and make it listen to britney spears music, and when it comes back it's really well behaved. I wonder if it's suffering from post tramatic stress syndrome now..
 
Probably not the select box... here's what I would do.

Code:
<?php

$temp = strlen($selected);
for ($i=0;$i<$temp;$i++) {
  echo $selected[$i].&quot;::&quot;.ord($selected[$i]);
  echo &quot;<br>&quot;;
}

?>

And run something like that on each string (DO NOT put them in the same loop. My guess is one of your strings has a newline at the end or something of that annoying sort.

-Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top