0

我有一个 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";


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

    }

  return implode( "\r\n", $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|

第二个和第三个 [10] 在不同的行上,与第一个不重合。我希望输出是:

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

整个字符串都在一行上,并用逗号分隔。我认为正在发生的事情是脚本将第二个和第三个 [10] 视为新条目,而不是连接到前一个数组。任何人都可以帮我解决这个问题。这将不胜感激!

4

2 回答 2

1

有些文本有一个简单的回车符\r,有些是换行符,有些文本\n既有回车符又有换行符\r\n。这取决于用于创建文本的编辑器。

您需要涵盖这些可能的情况。做这个:

return implode("\r",implode("\n",implode("\r\n",$data)));

于 2013-06-25T08:40:01.953 回答
0

您可以在不使用 implode 甚至临时数组等的情况下执行此操作,只需使用字符串连接即可。很可能也更快,但取决于你。

<?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";

function text_to_csv( $text = null ){
    $data   = null;
    $lines  = explode("\n",trim($text));

    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|
*/
?>
于 2013-06-25T08:46:03.137 回答