第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP讀取包含問答的txt文件并創(chuàng)建數(shù)組或?qū)ο螅?/h1>

我在moodle中看到了這個功能,他們上傳 .txt 文件并解析數(shù)據(jù)并創(chuàng)建問題庫。有些問題也有三個選項(xiàng)樣本 :-What is php?A. Php is a language.B. Php creates HTML.C. Php is fun.D. none of these.ANSWER: DWhich is YII-2?A. Framework.B. Tool.C. None of theseANSWER: A到目前為止,我已經(jīng)嘗試過解析它,但我不知道要實(shí)現(xiàn)批量上傳到底要做什么。txt_file    = file_get_contents('path/to/file.txt');$rows        = explode("\n", $txt_file);array_shift($rows);foreach($rows as $row => $data){}我正在嘗試獲取的數(shù)組[['question' => 'Php is a language.','options' => [             'A' =>  'Php is a language.',             'B' => 'Php creates HTML.',             'C' => 'Php is fun.',             'D' => 'none of these.'             ],'answer' => 'D'],.....]示例文件:-文件代碼fbSObnlghQkqroEk4lrQ
查看完整描述

1 回答

?
元芳怎么了

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超7個贊

嘗試(?mi)^(?!\h*(?:[A-Z]\h*\.|ANSWER\h*:))\h*(?<question>\S.*?)\??\h*\s*^\h*A\h*\.\h*(?<A>.*?)\h*\s*(?:^\h*B\h*\.\h*(?<B>.*?)\h*(?:\s*^\h*C\h*\.\h*(?<C>.*?)\h*(?:\s*^\h*D\h*\.\h*(?<D>.*?)\h*(?:\s*^\h*E\h*\.\h*(?<E>.*?)\h*)?)?)?)?\s*^\h*ANSWER\h*:\h*(?<answer1>[A-F])(?:\h*,\h*(?<answer2>[A-F]))?

循環(huán)匹配,從部分創(chuàng)建單個數(shù)組,
附加到更大的數(shù)組。
不需要“選項(xiàng)”,但匹配后可以從組 AE 創(chuàng)建子數(shù)組
,然后添加到單個數(shù)組。
重復(fù)下一場比賽。

php_demo

<?php

// Your code here!


$regex = '~(?mi)^(?!\h*(?:[A-Z]\h*\.|ANSWER\h*:))\h*(?<question>\S.*?)\??\h*\s*^\h*A\h*\.\h*(?<A>.*?)\h*\s*(?:^\h*B\h*\.\h*(?<B>.*?)\h*(?:\s*^\h*C\h*\.\h*(?<C>.*?)\h*(?:\s*^\h*D\h*\.\h*(?<D>.*?)\h*(?:\s*^\h*E\h*\.\h*(?<E>.*?)\h*)?)?)?)?\s*^\h*ANSWER\h*:\h*(?<answer1>[A-F])(?:\h*,\h*(?<answer2>[A-F]))?~';?


// Declare a string?

$nameString = 'What is php?


A. Php is a language.


B. Php creates HTML.


C. Php is fun.


D. none of these.


ANSWER: D


Linux is

A. A Graphic Software

B. A Driver

C. A Line Controller Software

D. An Operating System

ANSWER: D

Which feature helps in compress and contain the collection of files in to one

A. Zip

B. Shortcut

C. Icon

D. Extract

ANSWER: A



Which is YII-2?


A. Framework.


B. Tool.


C. None of these


ANSWER: A,B';?



// Use preg_match_all() function


$QA = []; // All questions


if ( preg_match_all($regex, $nameString, $matches, PREG_SET_ORDER) ) {

? ? foreach ($matches as $match) {

? ? ? ?$qa = [];? ?// Single question

? ? ? ?$qa['question'] = $match['question'];

? ? ? ?$qa['A'] = $match['A'];

? ? ? ?if ( array_key_exists( 'B', $match ) && $match['B'] )? $qa['B'] = $match['B'];

? ? ? ?if ( array_key_exists( 'C', $match ) && $match['C'] )? $qa['C'] = $match['C'];

? ? ? ?if ( array_key_exists( 'D', $match ) && $match['D'] )? $qa['D'] = $match['D'];

? ? ? ?if ( array_key_exists( 'E', $match ) && $match['E'] )? $qa['E'] = $match['E'];

? ? ? ?$qa['answer1'] = $match['answer1'];

? ? ? ?if ( array_key_exists( 'answer2', $match ) && $match['answer2'] )? $qa['answer2'] = $match['answer2'];


? ? ? ?array_push( $QA, $qa );

? ? }

}?

else {?

? ? echo("Could not find a single question");??

}?


print_R ( $QA);


?>

輸出


Array

(

? ? [0] => Array

? ? ? ? (

? ? ? ? ? ? [question] => What is php

? ? ? ? ? ? [A] => Php is a language.

? ? ? ? ? ? [B] => Php creates HTML.

? ? ? ? ? ? [C] => Php is fun.

? ? ? ? ? ? [D] => none of these.

? ? ? ? ? ? [answer1] => D

? ? ? ? )


? ? [1] => Array

? ? ? ? (

? ? ? ? ? ? [question] => Linux is

? ? ? ? ? ? [A] => A Graphic Software

? ? ? ? ? ? [B] => A Driver

? ? ? ? ? ? [C] => A Line Controller Software

? ? ? ? ? ? [D] => An Operating System

? ? ? ? ? ? [answer1] => D

? ? ? ? )


? ? [2] => Array

? ? ? ? (

? ? ? ? ? ? [question] => Which feature helps in compress and contain the collection of files in to one

? ? ? ? ? ? [A] => Zip

? ? ? ? ? ? [B] => Shortcut

? ? ? ? ? ? [C] => Icon

? ? ? ? ? ? [D] => Extract

? ? ? ? ? ? [answer1] => A

? ? ? ? )


? ? [3] => Array

? ? ? ? (

? ? ? ? ? ? [question] => Which is YII-2

? ? ? ? ? ? [A] => Framework.

? ? ? ? ? ? [B] => Tool.

? ? ? ? ? ? [C] => None of these

? ? ? ? ? ? [answer1] => A

? ? ? ? ? ? [answer2] => B

? ? ? ? )


)


查看完整回答
反對 回復(fù) 2023-04-15
  • 1 回答
  • 0 關(guān)注
  • 121 瀏覽

添加回答

了解更多

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號