修复PHP解析脚本输出?

时间:2013-06-25 16:24:00

标签: php regex parsing csv

我有一个PHP脚本解析纯文本,然后以CSV格式输出该文本,但我没有按照我需要的方式获得输出。我不确定如何解决它。

<?php
$text = "1. Bonus: Name some things about US history. For 10 points each:
[10] Name the first president of the United States of America.
ANSWER: George Washington
[10] How many original colonies were there?
ANSWER: 13
[10] How many states exist today?
ANSWER: 50

2. Bonus: Name some stuff. For 10 points each:
[10] What does lol mean?
ANSWER: Laugh Out Loud
[10] What is the capital of Virginia?
ANSWER: Richmond
[10] What language am I writing in?
ANSWER: PHP";


function text_to_csv( $text = null ) {
    $lines  = explode( "\n", $text );
    $data   = null;
    foreach( $lines as $line ) {
        $line = trim( $line );
        if ( empty( $line ) ) {
            continue;
        }
        if ( preg_match( '/^\[10\](.+?)$/', $line, $quest ) ) {
            $data .=  "|".trim( $quest[0] )."|,";
        }

        if ( preg_match( '/^([0-9]+)\.(.+?)$/', $line, $quest ) ) {
            $data .= "|".trim( $quest[1] )."|,";
            $data .= "|".trim( $quest[2] )."|,";
        }
        if ( preg_match( '/^ANSWER\:(.+?)$/', $line, $quest ) ) {
            $data .= "|".trim( $quest[1] )."|,";
        }
    }
    return rtrim($data,",");
}

echo text_to_csv( $text );

?>

这输出以下内容:

|1|,|Bonus: Name some things about US history. For 10 points each:|,|[10] Name the first president of the United States of America.|,|George Washington|,|[10] How many original colonies were there?|,|13|,|[10] How many states exist today?|,|50|,|2|,|Bonus: Name some stuff. For 10 points each:|,|[10] What does lol mean?|,|Laugh Out Loud|,|[10] What is the capital of Virginia?|,|Richmond|,|[10] What language am I writing in?|,|PHP|

整个字符串在一行上。我想在每个'奖励'套装之后打破它(如此):

|1|,|Bonus: Name some things about US history. For 10 points each:|,|[10] Name the first president of the United States of America.|,|George Washington|,|[10] How many original colonies were there?|,|13|,|[10] How many states exist today?|,|50|
|2|,|Bonus: Name some stuff. For 10 points each:|,|[10] What does lol mean?|,|Laugh Out Loud|,|[10] What is the capital of Virginia?|,|Richmond|,|[10] What language am I writing in?|,|PHP|

通过这种方式,每个“奖励”集都在一个单独的行上,并且在每一行中,这些段用逗号分隔(CSV格式)。任何人都可以帮助我实现这一目标。我真的很感激。我是解析和正则表达式的新手。

1 个答案:

答案 0 :(得分:0)

评论太长了,但答案时间不够长。

如果你想分开空行:

if ( empty( $line ) ) {
    continue;
}

应改为:

if ( empty( $line ) ) {
   $data .= "\n";
   continue;
}