现在我想检查这个文本框是否包含一个或两个单词,例如
if ($_POST['mytext'] == two words){
echo "That is Perfect";
} else{
echo "We don't accept this";
}
and I tried
if ($_POST['mytext'] > 1){
echo "That is Perfect";
} else{
echo "We don't accept this";
}
,它没有工作
这就是我的意思,那么怎么做呢?希望能找到一种方法。
谢谢
如果将两个单词定义为"一些字符后跟一个空格后跟一些字符"那么您可以这样做:
$mytext = $_POST["mytext"];
$parts = explode(" ", $mytext);
if (count($parts) !== 2) {
throw new Exception("too many or too little!");
}
if (strlen($parts[0]) === 0 || strlen($parts[1]) === 0) {
throw new Exception("not enough characters!");
}
请记住,这允许像"@ !"
使用str_word_count()
:
if (str_word_count($_POST['mytext']) > 1){
echo "That is Perfect";
} else{
echo "We don't accept this";
}
你可以使用
`substr_count('some text', ' ');
它将返回空格数,
try this
$text= preg_split(" ",$_POST['mytext']);
if (count($text) > 1){
echo "That is Perfect";
} else{
echo "We don't accept this";
}