Jump to content

[SOLVED] String length validator - $ works, £ doesn't - confused


webref.eu

Recommended Posts

Hi All

 

I have this code: 

 

if(strlen($Password) > 10) {
$ErrorMsg = $ErrorMsg . "Your Password is too long, please shorten it.<br>";
//need to prepare strings before putting them back in form field because of magic quotes and to handle special characters
$Password = PrepareForForm($Password);
$ConfirmPassword = PrepareForForm($ConfirmPassword);
} 

 

$Password is set by a form field.  If I input into the field: 

 

test$test$

 

The script is happy and I don't get an error, as expected because this string is not greater than 10 characters long. 

 

However, if I input: 

 

test£test£

 

I trip the error "Your Password is too long, please shorten it."  ??? I would not expect this because test£test£ is not great than 10 characters long, so what is happening? 

 

Many thanks all. 

Not sure

<?php
$Password = "test£test£";
if(strlen($Password) > 10) {
$ErrorMsg = $ErrorMsg . "Your Password is too long, please shorten it.<br>";
//need to prepare strings before putting them back in form field because of magic quotes and to handle special characters
$Password = PrepareForForm($Password);
$ConfirmPassword = PrepareForForm($ConfirmPassword);
}
else
{
echo "correct length";
}
?>

shows correct length on my page...

OK, so if I use this code: 

 

echo "Password before check: " . $Password . "<br>";
echo "strlen: " . strlen($Password) . "<br>";
echo "mb_strlen: " . mb_strlen($Password) . "<br>";
var_dump($Password);

 

... and enter the Password in my form field as: 

 

test£test£

 

The output I get is: 

 

Password before check: test£test£

strlen: 12

mb_strlen: 12

string(12) "test£test£"

 

I expected mb_strlen to give 10 not 12.  mb_strlen is giving the same result as strlen, why isn't it working? 

 

Thanks All for any advice. 

 

Rgds

 

 

OK, I found the solution, it was because I had the page encoding set to:

 

charset=utf-8

 

when the MySQL connection was using:

 

ISO-8859-1

 

changing the page encoding to:

 

charset=ISO-8859-1

 

... solved it.

 

Rgds

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.