Validation of the strings size

3

I was creating a function to validate large text fields (description, observation, ...), which in SQL will be saved as TEXT , so basically I did this:

function($valor, $min = 0, $max = 65000) {
    if (strlen($valor) >= $min || strlen($valor) <= $max) {
        return true;
    } else {
        return false;
    }
}

When I was looking for the maximum size of the fields of type TEXT of the I found this question of the SOen that responds to this, and it shows that the size of the text depends on the characters of the string. In fact the text types do not have a character limit but a byte limit, then two questions:

  • How to validate a text entry according to the maximum bytes size that the string should contain?

  • This validation is required only in fields TINYTEXT , TEXT , MEDIUMTEXT , and LONGTEXT ?

asked by anonymous 28.09.2018 / 15:37

1 answer

2

Use mb_strlen() .

But it's not that simple. This can be a problem for user usability. It should not bother you with implementation details. How are you going to tell him what to do with something he does not see? Either it will have to do an extremely complex algorithm or it will bother with attempts and errors. This is just one of the reasons I consider the use of multi-byte encodings an error.

Our industry has kind of defined that I would use UTF-8 by default, I prefer Latin-1 or something like that. Any encoding single byte works best in space occupancy and processing .

This is one reason to prefer VARCHAR who does not have this problem. If you think you can not use it, you'd prefer a% open size and be happy. But almost always LONGTEXT is an error.

One solution is to put a larger space. In general, in UTF-8, if double character placement is already well protected, unless it expects names in Chinese, Klingon or the like. That does not matter the bytes. I would go that way.

See more at:

28.09.2018 / 15:41