[PHP] Re: Comparing strings (revisited)
Clancy wrote: For some time I have been working on a text based database, in which each entry contains one or more lines of data, with the various fields delimited by semicolons, e.g. A;b;20GM;Restaurant;090508 n;;;Arintji;; a;Federation Square;;; p;9663 9900;;;9663 9901;;i...@arintji.com.au; All was going well but recently I decided to allocate every entry a unique identifier, and, in what with hindsight was clearly misguided enthusiasm, decided that each identifier should be a four digit base 36 number (the 20GM in the first line). This did not cause any problems until yesterday, when I tried to load a name beginning with 'R', and got the first name on the list. When I investigated I found that I was searching the array containing the data using: if ($ident == $data[$i]['group']['ident']) { .. I then found that I was searching for 20E2, but was getting a match on 2000. I tried 'if ((string) $ident == (string) $data[$i]['group']['ident'])', but this still matched. However 'if($ident === ' worked, as did 'if (!strcmp($ident, $data[$i])) {...'. After puzzling about this for a long time, I realised that the comparison process must have been treating the second value as a floating point number, and converting it to integer, or vice versa. (In floating point notation 20E2 = 20*10^^2 = 2000). I had thought that the (string) override meant to treat the actual value as a string, but in this case it must be converting the (assumed) actual value to a string, and then comparing the results. This surprised me considerably as it is clear from the results I achieve in other circumstances that the data is actually stored as a raw string. $data is a variable format array, and when the original data is read each line is exploded into a term of the data array: $data[][] = explode(';',$line[$i]);. If I print the value of the ident (or any other field) it is always shown as the original string, and when I save an updated version of the data, each term of the data array is imploded into a line of the data file in its original format. However if this value were actually converted to a floating point number when it was entered I would have to specify a format before I could write it out again, and as 20E2 is a rather non-standard format it is most unlikely that it would come out as this unaided. Is there any way to specify that each field is always to be treated as a string when I originally explode the input file into the data array? For someone brought up on rigidly defined data types dynamic typing can be very confusing! first you need to know what you're working with var_dump( array( $ident , $data[$i] ); and check the output for types, then do at various decent locations higher up the script till you find what's causing the problem. or post the source code in questions (however large) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Comparing Strings
Nils Holland wrote: My guess is that PHP only sees / compares the strings up to a certain position. Is this possible? And if so, is there any way around this, so that my whole string is seen? Greetings and thanks in advance, Nils Maybe it's interpreting something as a number somewhere. Did you try using === instead of == for your comparisons? Ah, thanks, that made it work! :-) PS: Why is your license key so insanely long, and why doesn't it use alphanumeric characters to reduce the length? Very good question, I should probably ask the person that came up with the way this license key works. Even if it has to be this long internally, I've been thinking about using some encoding / hashing technique to make it smaller at least when it's being passed to the PHP script. For the sake of testing, however, I didn't really care. Greetings and thanks again! Nils -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Comparing Strings
Nils Holland wrote: Hi folks, well, I have a really dumb question: I have the following script at http://www.tisys.org/misc/test.php: ?php echo $_GET['license']; ? Now I call it like this: http://www.tisys.org/misc/test.php?license=070177016081137173166041245224242038105254210007183127207103162117160180165221170166096217228128 And in fact, it echos the whole, long GET-string! Now i have the following script a tttp://www.tisys.org/misc/ol_license.php: ?php if ($_GET['license'] == 070177016081137173166041245224242038105254210007183127207103162117160180165221170166096217228128) { echo '1'; } else { echo '0'; } ? I launch it like: http://www.tisys.org/misc/ol_license.php?license=070177016081137173166041245224242038105254210007183127207103162117160180165221170166096217228128 And it returns a 1, which means: The strings match. Now I call it like http://www.tisys.org/misc/ol_license.php?license=170177016081137173166041245224242038105254210007183127207103162117160180165221170166096217228128 that is, I have changed the first number of my GET-String from a 0 to a 1 and indeed, the script returns 0, meaning: The scripts don't match. *HOWEVER*, no I call the script like: http://www.tisys.org/misc/test.php?license=070177016081137173166041245224242038105254210007183127207103162117160180165221170166096217228129 that is, I have changed the last number of the GET-String from an 8 to a 9, and interestingly, it returns 1 this time, which means the strings do match, which, however, they don't. My guess is that PHP only sees / compares the strings up to a certain position. Is this possible? And if so, is there any way around this, so that my whole string is seen? Greetings and thanks in advance, Nils Maybe it's interpreting something as a number somewhere. Did you try using === instead of == for your comparisons? Regards, Adam Zey. PS: Why is your license key so insanely long, and why doesn't it use alphanumeric characters to reduce the length? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: comparing strings - again!
This does not work although when I echo out the strings they are exactly the same. Strange! Ross [EMAIL PROTECTED] wrote in message news:... if (isset($_POST['Submit'])) { //echo post equals.$_POST['x']. corect is.$correct_answers[$page-1]; $compare1 = $_POST['x']; echo page is.$page; $compare2 = $correct_answers[($page-1)]; echo compare1 is .$compare1; echo BR; echo compare2 is .$compare2; if (strcmp($compare1, $compare2) == 0) { $incrementedPage = $page + 1; ? script type=text/javascript !-- window.location = http://www.google.com/; //-- /script ? //header(Location: evaluation.php?page=$incrementedPage); } else { $chances++; echo you have had.$chances; } } ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: comparing strings - again!
var_dump gives Company Director string(17) Company Director string(16) Why would they be different? Seems like they have add some extra whitespace? R. - Original Message - From: Jochem Maas [EMAIL PROTECTED] To: Ross [EMAIL PROTECTED] Cc: php-general@lists.php.net Sent: Tuesday, June 20, 2006 3:57 PM Subject: Re: [PHP] Re: comparing strings - again! Ross wrote: This does not work although when I echo out the strings they are exactly the same. Strange! try using var_dump($compare1, $compare2); Ross [EMAIL PROTECTED] wrote in message news:... if (isset($_POST['Submit'])) { //echo post equals.$_POST['x']. corect is.$correct_answers[$page-1]; $compare1 = $_POST['x']; echo page is.$page; $compare2 = $correct_answers[($page-1)]; echo compare1 is .$compare1; echo BR; echo compare2 is .$compare2; if (strcmp($compare1, $compare2) == 0) { $incrementedPage = $page + 1; ? script type=text/javascript !-- window.location = http://www.google.com/; //-- /script ? //header(Location: evaluation.php?page=$incrementedPage); } else { $chances++; echo you have had.$chances; } } ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: comparing strings - again!
Soted it was an extra whitespace character being added to the posted answer. The var_dump function highlighted it! nice Work Jochem. No idea where it campe from though! R. - Original Message - From: Jochem Maas [EMAIL PROTECTED] To: Ross [EMAIL PROTECTED] Cc: php-general@lists.php.net Sent: Tuesday, June 20, 2006 3:57 PM Subject: Re: [PHP] Re: comparing strings - again! Ross wrote: This does not work although when I echo out the strings they are exactly the same. Strange! try using var_dump($compare1, $compare2); Ross [EMAIL PROTECTED] wrote in message news:... if (isset($_POST['Submit'])) { //echo post equals.$_POST['x']. corect is.$correct_answers[$page-1]; $compare1 = $_POST['x']; echo page is.$page; $compare2 = $correct_answers[($page-1)]; echo compare1 is .$compare1; echo BR; echo compare2 is .$compare2; if (strcmp($compare1, $compare2) == 0) { $incrementedPage = $page + 1; ? script type=text/javascript !-- window.location = http://www.google.com/; //-- /script ? //header(Location: evaluation.php?page=$incrementedPage); } else { $chances++; echo you have had.$chances; } } ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: comparing strings - again!
[EMAIL PROTECTED] wrote: var_dump gives Company Director string(17) Company Director string(16) Why would they be different? probably because there is either: 1. white space in the value in your data source 2. white space being outputted along side the value when creating a form field weith that valu that and/or possible the fact that your doing a trim() in one place but not in another - personally I usually trim() incoming string data (regardless of any other validation/sanitation) to avoid stuff like this Seems like they have add some extra whitespace? exactly. btw: var_dump() should be showing exactly where the white space is e.g.: code: ?php $s1 = Company Director ; $s2 = Company Director; var_dump($s1, $s2); output: string(17) Company Director string(16) Company Director -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: comparing strings - again!
Ross wrote: This does not work although when I echo out the strings they are exactly the same. Strange! try using var_dump($compare1, $compare2); Ross [EMAIL PROTECTED] wrote in message news:... if (isset($_POST['Submit'])) { //echo post equals.$_POST['x']. corect is.$correct_answers[$page-1]; $compare1 = $_POST['x']; echo page is.$page; $compare2 = $correct_answers[($page-1)]; echo compare1 is .$compare1; echo BR; echo compare2 is .$compare2; if (strcmp($compare1, $compare2) == 0) { $incrementedPage = $page + 1; ? script type=text/javascript !-- window.location = http://www.google.com/; //-- /script ? //header(Location: evaluation.php?page=$incrementedPage); } else { $chances++; echo you have had.$chances; } } ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: comparing strings - again!
Jochem Maas wrote: [EMAIL PROTECTED] wrote: var_dump gives Company Director string(17) Company Director string(16) Why would they be different? probably because there is either: 1. white space in the value in your data source 2. white space being outputted along side the value when creating a form field weith that valu that and/or possible the fact that your doing a trim() in one place but not in another - personally I usually trim() incoming string data (regardless of any other validation/sanitation) to avoid stuff like this Seems like they have add some extra whitespace? exactly. btw: var_dump() should be showing exactly where the white space is e.g.: code: ?php $s1 = Company Director ; $s2 = Company Director; var_dump($s1, $s2); output: string(17) Company Director string(16) Company Director Without the quotes he's not going to see the extra white space though, as the browser is going to render the first space it comes too, but it will piss on any white space after that (for display purpose). Main reason I wrap my debugging data in pre/pre. -- John C. Nichel IV Programmer/System Admin (ÜberGeek) Dot Com Holdings of Buffalo 716.856.9675 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: comparing strings - again!
John Nichel wrote: Jochem Maas wrote: ... exactly. btw: var_dump() should be showing exactly where the white space is e.g.: code: ?php $s1 = Company Director ; $s2 = Company Director; var_dump($s1, $s2); output: string(17) Company Director string(16) Company Director Without the quotes he's not going to see the extra white space though, as the browser is going to render the first space it comes too, but it will piss on any white space after that (for display purpose). Main reason I wrap my debugging data in pre/pre. I use pre too when debugging in the browser - but I was testing on the cmdline, that said var_dump() outputs the quotes when outputting to the browser too. I wasn't 100% sure but I just checked to be safe ;-) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Comparing strings
In article [EMAIL PROTECTED], [EMAIL PROTECTED] (Phantom) wrote: In PHP I have tried if ($Action == Submit) but I get a parse error in that statement. What am I doing wrong? Thank you. I did an echo $Action to confirm that it's value is Submit. If the variable is set as you've confirmed, then that should work. If PHP is reporting a parse error with that line number, note that the lines where PHP stopped parsing isn't necessarily the line where the bad code resides. Try checking back a few lines; you may have left off a semi-colon or somesuch. Another idea: check that the variable's length is the expected 5 chars; maybe there's some trailing whitespace that's causing the comparison to fail (trim() is handy for avoiding this gotcha). If neither of those solve the problem, try re-posting with a larger code snippet. -- CC -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]