字符串最小、最大长度函数

发布于 2024-10-17 07:30:59 字数 1283 浏览 4 评论 0原文

我正在尝试检查字符串的最小和最大长度:

如果我向其传递一个数组,如下所示,我希望如果 true 则返回值,如果 false 则返回 NULL:

$lenghts = array('a' => array('min' => 20, 'max' => 70),
                 'b' => array('min' => 50, 'max' => 800),
                 'c' => array('min' => 3,  'max' =>8));

值是:

$values = array('thread_title' => 'this is it', 'thread_content' => 'this is not it', 'thread_tags' => 'also not it')

;

编辑

现在就像凌晨 5 点(真的很困),我应该复制并粘贴正确的版本,抱歉:

function string_min_max($string_array, $array)
{
   $returns = array();  # store returned values
   foreach ($array as $key)
   {
      #  check for minimum:
      if (array_key_exists('min', $key))
      {
         $minimum = (strlen($string_array[$key]) < $key['min'] ? $key = NULL : $key);
      }

      if (array_key_exists('max', $key))
      {
         $maximum = (strlen($string_array($key)) > $key['max'] ? $key = NULL : $key);
      }

      if ($minimum !== NULL && $maximum !== NULL)
      {
         $returns[$key]['min'] = $minimum;
         $returns[$key]['max'] = $maximum; 
      }
   }
}

这不起作用:

   string_min_max($values, $lengths);

I'm trying to check min and max length of strings:

If I pass it an array, such as follows, I want the values to be returned if true and NULL if false:

$lenghts = array('a' => array('min' => 20, 'max' => 70),
                 'b' => array('min' => 50, 'max' => 800),
                 'c' => array('min' => 3,  'max' =>8));

And the values are:

$values = array('thread_title' => 'this is it', 'thread_content' => 'this is not it', 'thread_tags' => 'also not it')

;

EDIT

It's like 5am here (really quite sleepy), I should copied and pasted the correct version, sorry:

function string_min_max($string_array, $array)
{
   $returns = array();  # store returned values
   foreach ($array as $key)
   {
      #  check for minimum:
      if (array_key_exists('min', $key))
      {
         $minimum = (strlen($string_array[$key]) < $key['min'] ? $key = NULL : $key);
      }

      if (array_key_exists('max', $key))
      {
         $maximum = (strlen($string_array($key)) > $key['max'] ? $key = NULL : $key);
      }

      if ($minimum !== NULL && $maximum !== NULL)
      {
         $returns[$key]['min'] = $minimum;
         $returns[$key]['max'] = $maximum; 
      }
   }
}

This does not work:

   string_min_max($values, $lengths);

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

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

发布评论

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

评论(3

心头的小情儿 2024-10-24 07:30:59

这段代码可以简化为:

function string_min_max($array)
{
   $returns = array();  # store returned values
   foreach ($array as $key)
   {
      #  check for minimum:
      if (TRUE)
      {
         $minimum = (FALSE ? $key = NULL : $key);
      }
   }
}

OR:

function string_min_max($array)
{
   $returns = array();  # store returned values
   foreach ($array as $key)
   {
      $minimum = $key;
   }
}

OR:

function string_min_max($array)
{
   $returns = array();  # store returned values
}

OR:

(void)

所以答案是:不。这是行不通的。

This code is reducable to:

function string_min_max($array)
{
   $returns = array();  # store returned values
   foreach ($array as $key)
   {
      #  check for minimum:
      if (TRUE)
      {
         $minimum = (FALSE ? $key = NULL : $key);
      }
   }
}

OR:

function string_min_max($array)
{
   $returns = array();  # store returned values
   foreach ($array as $key)
   {
      $minimum = $key;
   }
}

OR:

function string_min_max($array)
{
   $returns = array();  # store returned values
}

OR:

(void)

So the answer is: no. It won't work.

云醉月微眠 2024-10-24 07:30:59

如果不过多关注细节,一个明显的问题是您没有从函数中返回任何内容;您正在构建一个数组,但您不使用它,因此它会在函数结束时被销毁。

我认为至少你在函数结束时需要这个:

  return $returns;
}         // end function

然后你可以像这样调用你的函数:

$results = string_min_max($values, $lenghts);

Without looking too much at the details, one obvious problem is that you are not returning anyting from your function; you are building an array but you don´t use it so it gets destroyed at the moment your function ends.

I think that at least you will need this at the end of your function:

  return $returns;
}         // end function

You can then call your function like:

$results = string_min_max($values, $lenghts);
毅然前行 2024-10-24 07:30:59

如果使用它来按长度和字符验证字符串:

function checkString($string, $regex, $minlenght = 3, $maxlenght = 20) {
        if(preg_match($regex, $string) && strlen($string) >= $minlenght && strlen($string) <= $maxlenght) return true;
        return false;
}

if use this for validate string by length and chars :

function checkString($string, $regex, $minlenght = 3, $maxlenght = 20) {
        if(preg_match($regex, $string) && strlen($string) >= $minlenght && strlen($string) <= $maxlenght) return true;
        return false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文