php 检查字符串长度是否大于或小于所需数量的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5531599/
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
Function to check if string length in greater than or less than required amount
提问by johnathan ross
I want to create a function to check if the length of a string is greater than or less than a required amount:
我想创建一个函数来检查字符串的长度是否大于或小于所需的数量:
Something like this:
像这样的东西:
function check_string_lenght($string, $min, $max)
{
if ($string == "")
{
return x;
}
elseif (strlen($string) > $max)
{
return y;
}
elseif (strlen($string) < $min)
{
return z;
}
else
{
return $string;
}
}
The problem is I don't know what to return. I don't want to return something like 'String is too short'. Maybe a number, 0
if == ""
, 1
if greater than, 2
if less than?
问题是我不知道该返回什么。我不想返回诸如“字符串太短”之类的东西。也许一些,0
if == ""
,1
如果大于,2
如果低于?
What would be the proper way of doing this?
这样做的正确方法是什么?
回答by Felix Kling
You can return 1
, 0
and -1
like a lot of comparison functions do. In this case the return values could have these meanings:
您可以返回1
,0
并-1
想了很多比较的功能做。在这种情况下,返回值可能具有以下含义:
0
: The string length is inside the bounds-1
: too short1
: too long
0
: 字符串长度在边界内-1
: 太短1
: 太长
I don't think there is a proper wayfor this. You just have to document and explain the return values.
我认为这没有适当的方法。您只需要记录和解释返回值。
回答by Anax
I would have the function return a boolean value, where TRUE
would mean that the string is within limits and FALSE
would mean that the string length is invalid and change the part of code where the function is used.
我会让函数返回一个布尔值,其中TRUE
意味着字符串在限制范围内,FALSE
意味着字符串长度无效并更改使用函数的代码部分。
Furthermore, I would redesign the function as following:
此外,我将重新设计功能如下:
function is_string_length_correct( $string, $min, $max ) {
$l = mb_strlen($string);
return ($l >= $min && $l <= $max);
}
The part of code where the function is used could look like this:
使用该函数的代码部分可能如下所示:
if (!is_string_length_correct($string, $min, $max)) {
echo "Your string must be at least $min characters long at at
most $max characters long";
return;
}
回答by Sourav
if the length is lower than required then return 0 if more than required then -1 if within range then 1
如果长度小于要求则返回 0 如果超过要求则返回 -1 如果在范围内则返回 1
function check_string_lenght($string, $min, $max)
{
if (strlen($string)<$min)
return 0;
elseif (strlen($string) > $max)
return -1;
else
return 1;
}
回答by Adrian
function checkWord_len($string, $nr_limit) {
$text_words = explode(" ", $string);
$text_count = count($text_words);
for ($i=0; $i < $text_count; $i++){ //Get the array words from text
// echo $text_words[$i] ; "
//Get the array words from text
$cc = (strlen($text_words[$i])) ;//Get the lenght char of each words from array
if($cc > $nr_limit) //Check the limit
{
$d = "0" ;
}
}
return $d ; //Return the value or null
}
$string_to_check = " heare is your text to check"; //Text to check
$nr_string_limit = '5' ; //Value of limit len word
$rez_fin = checkWord_len($string_to_check,$nr_string_limit) ;
if($rez_fin =='0')
{
echo "false";
//Execute the false code
}
elseif($rez_fin == null)
{
echo "true";
//Execute the true code
}