Exemplo n.º 1
0
    $str_array = explode(".", $str);
    unset($str_array[0]);
    //return  trim(implode(".",$str_array));
    return html_entity_decode(trim(implode(".", $str_array)));
}
function process_answer($str)
{
    $str = trim($str);
    $f_str = preg_replace("/\\([A-Da-d]\\)/", "", substr($str, 0, 3)) . substr($str, 3);
    return html_entity_decode(trim($f_str));
}
// Retrieve the DOM from a given URL
//$html = file_get_html('file:///var/www/html/quizmaster/upload/uk.html');
$html = file_get_html('/var/www/html/quizmaster/upload/uphjsc12.html');
//echo $html; exit;
$out = fopen('/var/www/html/quizmaster/upload/new.csv', 'w');
fputcsv($out, array('question', 'option_1', 'option_2', 'option_3', 'option_4', 'correct_answer', 'category', 'explanation', 'multi_answer'));
// Find all "span" tags and print their HREFs
foreach ($html->find('span') as $element) {
    // echo $element. '<br>';
    $keywords = explode("<br />", $element);
    //question,option_1,option_2,option_3,option_4,correct_answer,category,explanation,multi_answer
    $cnt = count($keywords);
    for ($i = 3; $i < $cnt; $i = $i + 6) {
        fputcsv($out, array(process_question($keywords[$i]), process_answer($keywords[$i + 1]), process_answer($keywords[$i + 2]), process_answer($keywords[$i + 3]), process_answer($keywords[$i + 4]), '', '', '', ''));
    }
    pr($keywords);
    echo "---------------------<br>";
}
fclose($out);
exit;
    return "";
}
// Retrieve the DOM from a given URL
//$html = file_get_html('file:///var/www/html/quizmaster/upload/uk.html');
$html = nl2br2(file_get_contents('/var/www/html/quizmaster/upload/tobeuploaded/UPHJS Pre 2014 | Uttar Pradesh Higher Judicial Service (Preliminary) Examination- 2014 text.html'));
//echo $html;
$htmlArray = explode("<br />", $html);
$out = fopen('/var/www/html/quizmaster/upload/new.csv', 'w');
fputcsv($out, array('question', 'option_1', 'option_2', 'option_3', 'option_4', 'correct_answer', 'category', 'explanation', 'multi_answer'));
// Find all "span" tags and print their HREFs
$cnt = count($htmlArray);
$i = 0;
while ($i < $cnt) {
    $i++;
    $data = array();
    $data['question'] = process_question($htmlArray[$i++]);
    $data['option_1'] = process_answer($htmlArray[$i++]);
    $data['option_2'] = process_answer($htmlArray[$i++]);
    $data['option_3'] = process_answer($htmlArray[$i++]);
    $data['option_4'] = process_answer($htmlArray[$i++]);
    $i++;
    $data['correct_answer'] = process_answer_option($htmlArray[$i++]);
    $data['category'] = "6";
    $data['explanation'] = "";
    $data['multi_answer'] = "";
    pr($data);
    //question,option_1,option_2,option_3,option_4,correct_answer,category,explanation,multi_answer
    fputcsv($out, array($data['question'], $data['option_1'], $data['option_2'], $data['option_3'], $data['option_4'], $data['correct_answer'], $data['category'], '', ''));
    echo "---------------------<br>";
}
fclose($out);