webref.eu Posted August 2, 2008 Share Posted August 2, 2008 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. Link to comment https://forums.phpfreaks.com/topic/117865-solved-string-length-validator-works-%C2%A3-doesnt-confused/ Share on other sites More sharing options...
papaface Posted August 2, 2008 Share Posted August 2, 2008 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... Link to comment https://forums.phpfreaks.com/topic/117865-solved-string-length-validator-works-%C2%A3-doesnt-confused/#findComment-606246 Share on other sites More sharing options...
Barand Posted August 2, 2008 Share Posted August 2, 2008 try a var_dump($Password); to check content Link to comment https://forums.phpfreaks.com/topic/117865-solved-string-length-validator-works-%C2%A3-doesnt-confused/#findComment-606247 Share on other sites More sharing options...
Stooney Posted August 2, 2008 Share Posted August 2, 2008 £ is a multi-byte character. To count it properly use mb_strlen() http://us.php.net/manual/en/function.mb-strlen.php Link to comment https://forums.phpfreaks.com/topic/117865-solved-string-length-validator-works-%C2%A3-doesnt-confused/#findComment-606268 Share on other sites More sharing options...
webref.eu Posted August 3, 2008 Author Share Posted August 3, 2008 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 Link to comment https://forums.phpfreaks.com/topic/117865-solved-string-length-validator-works-%C2%A3-doesnt-confused/#findComment-606705 Share on other sites More sharing options...
webref.eu Posted August 3, 2008 Author Share Posted August 3, 2008 Sorry guys but I'm still struggling with this. Rgds Link to comment https://forums.phpfreaks.com/topic/117865-solved-string-length-validator-works-%C2%A3-doesnt-confused/#findComment-606832 Share on other sites More sharing options...
webref.eu Posted August 4, 2008 Author Share Posted August 4, 2008 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 Link to comment https://forums.phpfreaks.com/topic/117865-solved-string-length-validator-works-%C2%A3-doesnt-confused/#findComment-607505 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.