检查PHP中的字符串长度 [英] Check string length in PHP

查看:78
本文介绍了检查PHP中的字符串长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个长度为141个字符的字符串.使用以下代码,如果字符串大于或小于140,我有一个if语句返回一条消息.

I have a string that is 141 characters in length. Using the following code I have an if statement to return a message if the string is greater or less than 140.

libxml_use_internal_errors(TRUE);
$dom = new DOMDocument();
$dom->loadHTMLFile($source);
$xml = simplexml_import_dom($dom);
libxml_use_internal_errors(FALSE);
$message = $xml->xpath("//div[@class='contest']");

if (strlen($message) < 141)
{
   echo "There Are No Contests.";
}
elseif(strlen($message) > 142)
{
   echo "There is One Active Contest.";
}

我在$ message上使用了var_dump,它显示字符串为[0]=> string(141).这是我的问题:当我将if语句的数字更改为<130>131时,尽管字符串大于131,它仍然返回第一条消息.

I used var_dump on $message and it shows the string is [0]=> string(141). Here is my problem: When I change the numbers for the if statement to <130 and >131, it still returns the first message, although the string is greater than 131.

无论我使用的数字少于141,我总是得到没有比赛".回到我身边.

No matter what number I use less than 141 I always get "There Are No Contests." returned to me.

推荐答案

尝试使用常见语法:

if (strlen($message)<140) {
    echo "less than 140";
}
else
    if (strlen($message)>140) {
        echo "more than 140";
    }
    else {
        echo "exactly 140";
    }

这篇关于检查PHP中的字符串长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆