如何检查帖子字段是否长度为8 - 20个字符[重复]

Possible Duplicate:
PHP strlen question

how to check if text from post field is 8 - 20 characters long? Is it done with strlen, and could you give me some example, please?

$variable = $_POST['your-field-name']; // or $_GET['']; depends on your post method

if(strlen($variable) > 7 && strlen($variable) < 21) {
 return true;
} else {
 return false;
}

It's easy:

$item = $_POST['item'];
if (strlen($item) >= 8 && strlen($item) <= 20) { /*do something*/ }

Good luck! Konstantin

<?php

// get the length of the text. we take the value by reference
// because there is the risk it doesn't exist (such as when
// no data is submitted).
$textLen = strlen($myTextField = &$_POST['my_text_field']);

// test its length
if ($textLen >= 8 && $textLen <= 20) {
    echo 'Text is the right length.';
} else {
    echo 'Text is the wrong length.';
}

The easiest option is to use strlen:

strlen($_POST['shinyHappyString']) > 7 && strlen($_POST['shinyHappyString']) < 21

However, if you encounter multi-byte strings, you are in trouble. Therefore I recommend you to use grapheme_strlen or mb_strlen instead. You can check how they use it in Symfony2's validators.