【问题标题】:How to validate the format of a string?如何验证字符串的格式?
【发布时间】:2013-08-23 12:42:13
【问题描述】:

基本上,我有一个通过 foreach 循环从多维数组构建的变量。

我需要以某种方式验证它的格式,虽然我不确定如何去做,因为我还是个 PHP 新手。

这就是我的字符串的样子:

question1,answer1,answer2,answer3,answer4
question2,answer1,answer2,answer3,answer4
question3,answer1,answer2,answer3,answer4

我需要验证确切的格式,即:

string[comma]string[comma]string[comma]string[comma]string
string[comma]string[comma]string[comma]string[comma]string
string[comma]string[comma]string[comma]string[comma]string

如果可能的话,我需要用一个布尔变量来验证它,如果它与格式匹配则返回真或假。这些字符串可以包含任何内容,例如字母、数字、特殊字符等。

每行总是有 5 个字符串和 4 个逗号,不会多也不会少。

如果需要,这是构建多维数组然后将其转换为字符串的代码。它正在抓取上传的 CSV 文件,将其转换为多维数组,然后构建字符串。如您所见,我的数组是从 1 而不是 0 开始的,没有必要解释原因,因为那是题外话。

$csv_array = array(array());
    if (!empty($_FILES['upload_csv']['tmp_name'])) 
    {
        $file = fopen($_FILES['upload_csv']['tmp_name'], 'r');
    }

    if($file)
    {
        while (($line = fgetcsv($file)) !== FALSE) 
        {
            $csv_array[] = array_combine(range(1, count($line)), array_values($line));
        }

        fclose($file);
    }


    if(!empty($csv_array[1])) 
    {   
        foreach (array_slice($csv_array,1) as $row) 
        {
            $all_questions = $all_questions . $row[1] . ",";
            $all_questions = $all_questions . $row[2] . ",";
            $all_questions = $all_questions . $row[3] . ",";
            $all_questions = $all_questions . $row[4] . ",";
            $all_questions = $all_questions . $row[5];
            $all_questions = $all_questions . "\n";
        }
    }

非常感谢所有帮助。

提前致谢!

【问题讨论】:

  • 使用explode(),并计算结果数组有多少项?但这只有在字符串中没有逗号的情况下才有效。
  • if (count($line) == 5)
  • @andrewsi OP 已经在使用fgetcsv(),所以他不会有这个问题。

标签: php validation


【解决方案1】:

听起来正则表达式可以完成这项工作:

$match_ok = preg_match('/^([^,]+,){4}[^,]+$/', $test_string);

如果您想让[string] 为空,您可以将所有+ 更改为*

解释:第一个^ 匹配行首,然后是一个或多个不是逗号的字符,后面应该是逗号。这个序列必须有 4 次,并且后面必须跟另一个不以逗号结尾的字符串。 $ 匹配行尾。

如果你想匹配while多行字符串,你可以使用:

$match_ok = preg_match('/^(([^,]+,){4}[^,]+\n){3}$/', $test_string);

(因为preg_match默认在多行模式下工作)

编辑 2:您甚至可以通过单独处理最后一行来使正则表达式匹配不以换行符结尾的字符串,就像使用 cols 一样:

$match_ok = preg_match('/^(([^,]+,){4}[^,]+\n){2}([^,]+,){4}[^,]+$/', $test_string);

【讨论】:

  • 这会失败,因为我的字符串有换行符,就像$test_string = "question1,answer1,answer2,answer3,answer4\nquestion2,answer1,answer2,answer3,answer4\nquestion3,answer1,answer2,answer3,answer4";
  • 当我用你的正则表达式运行它时,它会回显bad。知道为什么吗? <?php $test_string = "question1,answer1,answer2,answer3,answer4\nquestion2,answer1,answer2,answer3,an‌​swer4\nquestion3,answer1,answer2,answer3,answer4"; if(preg_match('/^(([^,]+,){4}[^,]+\n){3}$/', $test_string)) { echo "good"; } else { echo "bad"; } ?>
  • 抱歉,字符串是否以换行符结尾?正则表达式匹配以换行符结尾的字符串
  • 不幸的是,没有。字符串末尾不会有空行。
  • 刚刚修改了我的其余代码,使字符串以空换行符结尾。很容易,因为我需要做的就是稍微改变我的循环。您的正则表达式现在可以完美运行,感谢您的帮助!
【解决方案2】:

试试这个简单的,可能有很多其他解决方案可以解决这个问题

 <?php
 $test_string = "question1,answer1,answer2,answer3,answer4";
 $newarray=explode(',',$test_string);
 if(count($newarray)=='5'){
    your code......;
   }
 ?>

--------真假-------------

<?php
  $test_string = "questionAsdAD1###,234234,answer2,answer3,answer4";
   function ToCheckMyStrung($test_string){
    $newarray=explode(',',$test_string);
    if(count($newarray)=='5'){
       return true;
    } else {
      return false;
     }
  }
   ?>

【讨论】:

  • 伟大的想法。虽然,它不测试每个问题中的换行符?例如,有 5 个字符串和 4 个逗号,然后是换行符,然后序列重复。
  • 这会失败,因为我的字符串可以有换行符,就像$test_string = "question1,answer1,answer2,answer3,answer4\nquestion2,answer1,answer2,answer3,an‌​swer4\nquestion3,answer1,answer2,answer3,answer4";
  • 另外,如果我将 returns 替换为 echos,则不会出现任何内容。我相信您的代码中某处存在错误。
  • 你有记录你的字符串带断线的次数吗?
  • 如果我在\n 使用explode 然后在, 再次爆炸。
【解决方案3】:

在foreach中 使用

implode(',',$row); for proper formatting. 

【讨论】:

    【解决方案4】:

    您可以在创建字符串变量时执行此操作,

    $count=1;
    $trace=array();
    if(!empty($csv_array[1])) 
    {   
        foreach (array_slice($csv_array,1) as $row) 
        {
            $all_questions = $all_questions . $row[1] . ",";
            $all_questions = $all_questions . $row[2] . ",";
            $all_questions = $all_questions . $row[3] . ",";
            $all_questions = $all_questions . $row[4] . ",";
            $all_questions = $all_questions . $row[5];
            $all_questions = $all_questions . "\n";
            if($row[1]=="" || $row[2]=="" || $row[3]=="" || $row[4]=="" || $row[5]=="")
            { 
                  $trace[]=$count;
            }
            $count++;
        }
    }
    

    而不仅仅是随时使用 $trace 数组。

    【讨论】:

      【解决方案5】:

      正则表达式会帮助你:

      $test_string = "Some,string,you,want,to,test";
      if(preg_match('@^([a-zA-Z0-9]+\,)+([a-zA-Z0-9])+$@', $test_string)) {
          echo 'All ok';
      }
      

      【讨论】:

      • 如何确保正好有五个字符串和四个逗号?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-15
      • 2014-03-25
      • 2011-11-28
      • 2020-10-27
      • 1970-01-01
      • 1970-01-01
      • 2021-06-20
      相关资源
      最近更新 更多