0

That's my code

$devmsg = $db->real_escape_string($_POST['devmsg']);
if(strlen($devmsg) <= 200) {
do something
} else {
do some other thing
}

When I add to the message some special chars such as "(" "[" "." "{" and so on the strlen() counts wrong.. If my message is ~160-180 characters long and there are specials chars in it , the if operator just returns false and goes to else.. I thought the problem's here $db->real_escape_string and then removed it but still got the same result.

tsvmitev
  • 69
  • 9

1 Answers1

3

Might be a UTF-8 problem.

Try to pass the text through utf8_decode() first:

<?php 
$length = strlen(utf8_decode($s)); 
?> 

utf8_decode() converts characters that are not in ISO-8859-1 to '?', which, for the purpose of counting, is quite alright.

Hebrus
  • 133
  • 6