0

I got string that I split using 'explode()'.But I need some more steps to do.I have to split that array output few more times by delimiter.

The txt file's contents look like

 Question1|opt1,opt2,opt3;Question2|opt1,opt2,opt3;question3|opt1,opt2,opt3;Question4|opt1,opt2,opt3;

Please have a look at the code

$open=fopen("mydata.txt","r+");
  while(!feof($open)){
     $see=fgets($open);
     $exp=explode(";",$see);
     //$exp1=explode("|",$exp);
     //$exp2=explode(",",$exp1);

     $vaq = "</br> ".$imp[0];
     echo $vaq;

    }
fclose($open);

what I need is to echo in following format

Question1
Opt1
Opt2
Opt3

Question2
Opt1
Opt2
Opt3

.......
.......
....... 

I know 'explode()' split string and output array.but even after that tried to see the result.It gives me error.

Look forward for expert assistance.

Thanks in advance.

Salim Khan
  • 53
  • 1
  • 12

1 Answers1

2
$str = file_get_contents('mydata.txt');
$data = preg_split('/[\|,;]/', $str);
echo join("\n<br>", $data);

Code assumes that non of QuestionN, OptN have symbols |, , and ; inside.

If you want to separate all the things

$blocks = explode(';', file_get_contents('mydata.txt'));
foreach($blocks as $block)
{
    list($question, $opts) = explode('|', $block);
    $opts = explode(',', $opts);

    // do whatever you wanted to do
    var_dump($question);
    var_dump($opts);
}
Cheery
  • 16,063
  • 42
  • 57