简体   繁体   中英

How to validate the format of a string?

Basically, I have a variable that is built from a multidimensional array via a foreach loop.

I need to somehow validate the format that it is in, although I am not to sure how to go about this since I am still quite a newbie when it comes to PHP.

This is how my string will look:

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

I need to validate that exact format, being:

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

I need to validate it with a boolean variable if possible, to return true or false if it matches the format or not. These strings can contain anything such as letters, numbers, special characters, etc.

Each line will ALWAYS have 5 strings and 4 commas, never more and never less.

If needed, this is the code that builds the multidimensional array and then converts it into a string. It is grabbing an uploaded CSV file, transferring it into a multidimensional array, and then building the string afterwards. As you can see, my array is starting at 1 instead of 0, no point explaining why since that is off topic.

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

All help is greatly appreciated.

Thanks in advance!

Sounds like a regex could do the job:

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

If you want to allow [string] to be empty, you may change all + to * .

Explanation: The first ^ matches the line start, then one or more characters, that are not commas, are expected followed by a comma. This sequence has to be there 4 times and has to be followed by another string not terminated by a comma. The $ matches the end of the line.

If you want to match the while multiline string, you may use:

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

(since preg_match works in multiline mode by default)

Edit 2: You may even make the regex match to a string not ending with a newline by handling the last line separately, just as it is done with the cols:

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

Try This simple one, May be so many other solutions possible for this

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

--------------------TRUE FALSE-------------

<?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. 

You can do it while you are creating your string variable,

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

and than just make use of $trace array whenever you want.

A regular expression will help you out:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM