如何验证字符串的格式


How to validate the format of a string?

基本上,我有一个通过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

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

每行总是有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";
        }
    }

我们非常感谢所有的帮助。

提前感谢!

听起来regex可以完成这项工作:

$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);

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

 <?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;
     }
  }
   ?>
foreach中的

使用

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

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

$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数组。

正则表达式将帮助您:

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