nano Posted May 26, 2011 Share Posted May 26, 2011 I am truly stumped on this one.. I have an array that contains the string: "185 - PGB_4364 - 7 x 5 Inches - (£3.00)" and on a page I have a hidden input with the value of: "185 - PGB_4364 - 7 x 5 Inches - (£3.00)" However when doing a var_dump() - it is telling me they are different lengths? off by 5.. I am trying to do if the string == the $_post then do something, however they never equal the same because of the length difference? a difference I just can't see! var_dump($item['name']); string(45) "185 - PGB_4364 - 4 x 6 Inches - (£2.00)" var_dump($_POST['product']); string(40) "185 - PGB_4364 - 4 x 6 Inches - (£2.00)" Has anyone come across this before? And if so, what would be the best way to resolve this.. I need both strings to match each other Here is my code, there the code never gets inside the 2nd if: if ($_POST['remove']) { $products = $_SESSION['purchase']; foreach ($products as $key => $item) { if ($item['name'] == $_POST['product']){ unset($products[$key]); } } $_SESSION['purchase'] = $products; } Cheers Quote Link to comment https://forums.phpfreaks.com/topic/237570-a-strange-issue-with-string-length/ Share on other sites More sharing options...
requinix Posted May 26, 2011 Share Posted May 26, 2011 It's likely an issue with text encoding. 1. Compare product IDs, not product names. 2. If you don't feel like doing that, try sprinkling in some utf8_decode. 3. If neither of those are good for you, what page encoding are you using? If you're not sure, what does bin2hex say the two strings are? Quote Link to comment https://forums.phpfreaks.com/topic/237570-a-strange-issue-with-string-length/#findComment-1220826 Share on other sites More sharing options...
nano Posted May 26, 2011 Author Share Posted May 26, 2011 Unreal.. I was sitting here for the past 2 hours thinking where has the 5 extra characters come from.. then it hit me.. the pound sign.. (£). I removed that and then both strings matched in length.. it must be the way an input value is stored or text encoding as you mention. An odd error but I'm glad it's resolved! Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/237570-a-strange-issue-with-string-length/#findComment-1220831 Share on other sites More sharing options...
xyph Posted May 26, 2011 Share Posted May 26, 2011 You can get around this by using htmlentities() before echo'ing into your hidden form field's value Quote Link to comment https://forums.phpfreaks.com/topic/237570-a-strange-issue-with-string-length/#findComment-1220834 Share on other sites More sharing options...
Psycho Posted May 26, 2011 Share Posted May 26, 2011 EDIT: I was working on this when the last few posts were made, but I'll submit anyway as the info would be helpful for others debugging a similar issue ---------------------------------------------------------- When you use var_dump() don't just look at what is presented in the browser - check the actual HTML source. I have a suspicion that the "£", or another character is actually a character code in the $POST value. Example: $text = "This is a <string> of text"; var_dump($text); What shows in the browser string(32) "This is a >string< of text" What is in the actual HTML source string(32) "This is a >string< of text" Quote Link to comment https://forums.phpfreaks.com/topic/237570-a-strange-issue-with-string-length/#findComment-1220835 Share on other sites More sharing options...
nano Posted May 26, 2011 Author Share Posted May 26, 2011 Thanks guys, appreciate the extra info Quote Link to comment https://forums.phpfreaks.com/topic/237570-a-strange-issue-with-string-length/#findComment-1220840 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.