Jump to content

A strange issue with string length..


nano

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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"

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.