valid characters for my strings

As a convention for my code: All strings coming from a web form or a file will be validated for compliance with restrictions in this post.

The code and concepts in this post operate on the principal that characters are encoded in single bytes simply by placing their code point in a byte. As of the current version of PHP, that is how characters are are assumed to be and how they are processed.

I don’t want my web application to process any strings which have bytes/characters whose value is 128 or higher. In other words I don’t want strings which contain non-US keyboard characters The reason is that I don’t want to be concerned with the following complications:

  • ISO-8859-1 being incompatible with UTF-8
  • Characters that cause browsers to mess up when UTF-8 gets translated to ISO-8859-1.
  • PHP functions unable to properly deal with multi-byte UTF-8 strings.

The code which follows tests whether a string fits this description. It does this by making sure every byte/character in the string is not in the range 128-255. This code was posted on stackoverflow.com by RobertPitt.

You may be saying: What about the bytes belonging to a multi-byte character? I say: Those bytes will fail the test; and, those characters are not allowed in my web apps.

$chars = range(128,255);
$string = "testing a plain string";
for($s=0;$s<strlen($string);$s++)
{
    if(in_array(ord($string[$s]),$chars))
    {
        //Invalid
    }
}

If one day my code is run on a version of PHP which accesses actual characters rather than single bytes of a string then this code may not work.

Advertisements

About samehramzylabib

See About on https://samehramzylabib.wordpress.com
This entry was posted in Character Encoding, Coding, PHP and Filesystem, PHP Form Processing and tagged , , , , , , , . Bookmark the permalink.

Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s