在 PHP 中检查字符串长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5563152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:49:30  来源:igfitidea点击:

Check string length in PHP

phpstring

提问by FAFAFOHI

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

我有一个长度为 141 个字符的字符串。使用以下代码,if如果字符串大于或小于 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.";
}

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 ifstatement to <130and >131, it still returns the first message, although the string is greater than 131.

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

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

无论我使用什么数字小于 141,我总是得到“没有比赛”。回到我身边。

回答by nicola

Try the common syntax instead:

尝试使用通用语法:

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

回答by Poelinca Dorin

[0]=> string(141)means $message is an array so you should do strlen($message[0]) < 141...

[0]=> string(141)意味着 $message 是一个数组,所以你应该做strlen($message[0]) < 141...

回答by Emmerman

[0]=> string(141)means that $message is an array, not string, and $message[0] is a string with 141 characters in length.

[0]=> string(141)表示 $message 是一个数组,而不是字符串,而 $message[0] 是一个长度为 141 个字符的字符串。

回答by ZeissS

$messageis propably not a string at all, but an array. Use $message[0]to access the first element.

$message可能根本不是字符串,而是数组。使用$message[0]访问的第一要素。

回答by Evert

The xpath()function does not return a string. It returns an array with XML elements (of type SimpleXMLElement), which may be casted to a string.

XPath的()函数不返回一个字符串。它返回一个包含 XML 元素(类型为SimpleXMLElement)的数组,该数组可以转换为字符串。

if (count($message)) {
   if (strlen((string)$message[0]) < 141) {
      echo "There Are No Contests.";
   }
   else if(strlen((string)$message[0]) > 142) {
      echo "There is One Active Contest.";
   }
}

回答by Capitaine

Because $xml->xpathalways return an array, and strlenexpects a string.

因为$xml->xpath总是返回一个数组,并strlen期望一个字符串

回答by Dimitre Novatchev

An XPath solution is to use:

XPath 解决方案是使用

string-length((//div[@class='contest'])[$k])

where $k should be substituted by a number.

其中 $k 应替换为数字。

This evaluates to the string length of the $k-th (in document order) divin the XML document that has a classattribute with value 'contest'.

这计算为divXML 文档中第 $k 个(按文档顺序)的字符串长度,该文档class具有值为 'contest'的属性。