在 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
Check string length in PHP
提问by FAFAFOHI
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.
我有一个长度为 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 if
statement to <130
and >131
, it still returns the first message, although the string is greater than 131.
我在 $message 上使用了 var_dump,它显示字符串是[0]=> string(141)
. 这是我的问题:当我将if
语句的数字更改为<130
and 时>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
$message
is 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->xpath
always return an array, and strlen
expects 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) div
in the XML document that has a class
attribute with value 'contest'.
这计算为div
XML 文档中第 $k 个(按文档顺序)的字符串长度,该文档class
具有值为 'contest'的属性。