3
I was creating a function to validate large text fields (description, observation, ...), which in SQL will be saved as TEXT
, so I basically did this:
function($valor, $min = 0, $max = 65000) {
if (strlen($valor) >= $min || strlen($valor) <= $max) {
return true;
} else {
return false;
}
}
When I went to find the maximum size of the type fields TEXT
of the found that question of the Soen that responds to this, and it shows that the size of the text depends on the characters in the string. Actually text types do not have a character limit but a byte limit, so two questions:
How to validate a text entry according to the maximum byte size that that string should contain?
This validation is only required in fields
TINYTEXT
,TEXT
,MEDIUMTEXT
, andLONGTEXT
?
I didn’t add the tag
mysql
because I believe the question is relevant to more than one database– Costamilam
I don’t understand this logic, if
$valor
is 0 (zero) must returntrue
?– user60252
If the value size is greater than the minimum and less than the maximum @Leocaracciolo
– Costamilam
https://ideone.com/YOph1P
– user60252
@Leocaracciolo
$valor
is the text– Costamilam
I actually meant $value="";
– user60252
The tag is fundamental, because the Dbs handle it differently. Mysql, for example, does not measure CHAR and VARCHAR in bytes, and yes and characters and this can influence the details of the answers. Encoding is also fundamental, because this concern of yours only makes a difference in multibyte encodings (which unfortunately are used for no reason by most developers - programmer who knows what he’s doing is a minority, the rest is just a fashion follower).
– Bacco
For example, if you don’t need support for emoticons (which are usually silly in most applications) and you don’t use Eastern languages, Latin 1 has only advantages. Faster, less space, all Latin language characters, etc.
– Bacco
@Bacco in case use UTF-8
– Costamilam
And do you need it? Another thing. What’s the text for if your limit is 65000 bytes? And if you need more, use a LONGTEXT then. All these variable formats, however much you abuse them, will spend at most 4 bytes more. 65000 bytes fits in Varchar, which gives more than 15000 UTF8 characters of the most complex
– Bacco
In other words: if you use Latin 1 (it can only be in that field, Mysql allows) you gain 4x the space limit, or if you use UTF-8 with 15000 limit fits in varchar, or if you use LONGTEXT you can limit to 1/4 of the maximum value without fear. And no Ifs for nothing.
– Bacco
@Bacco was worth the explanation, in the end I will use the
VARCHAR
, I’ll study them some more encoding– Costamilam