检查字符串长度是否大于或小于所需量的函数

发布于 2024-10-30 05:41:45 字数 464 浏览 3 评论 0原文

我想创建一个函数来检查字符串的长度是否大于或小于所需的数量:

像这样的事情:

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;
 }

}

问题是我不知道要返回什么。我不想返回“字符串太短”之类的内容。也许是一个数字,0 if == ""1 如果大于,2 如果小于?

这样做的正确方法是什么?

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?

What would be the proper way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

淑女气质 2024-11-06 05:41:45

您可以像许多比较函数一样返回 10-1。在这种情况下,返回值可能具有以下含义:

  • 0:字符串长度在范围内
  • -1:太短
  • 1:太长

我认为没有适当的方法来做到这一点。您只需记录并解释返回值。

You can return 1, 0 and -1 like a lot of comparison functions do. In this case the return values could have these meanings:

  • 0: The string length is inside the bounds
  • -1: too short
  • 1: too long

I don't think there is a proper way for this. You just have to document and explain the return values.

我偏爱纯白色 2024-11-06 05:41:45

我会让函数返回一个布尔值,其中 TRUE 表示字符串在限制范围内,FALSE 表示字符串长度无效并更改代码部分使用该函数的地方。

此外,我将重新设计该函数,如下所示:

function is_string_length_correct( $string, $min, $max ) {

    $l = mb_strlen($string);
    return ($l >= $min && $l <= $max);
}

使用该函数的代码部分可能如下所示:

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;
}

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.

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;
}
梦旅人picnic 2024-11-06 05:41:45

如果长度小于要求则返回 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;
}

if the length is lower than required then return 0 if more than required then -1 if within range then 1

function check_string_lenght($string, $min, $max)
{
 if (strlen($string)<$min)
   return 0;   
 elseif (strlen($string) > $max)
   return -1;
 else
   return 1;
}
对不⑦ 2024-11-06 05:41:45
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
}
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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文