0

我无法编写从文本中提取某些数据所需的代码。我的文字是这样组织的:

[class name]. [class name]. (class units). [class description]. [class instructors]

例如:

200A-200B。民事诉讼。(三)法典制度和联邦规则下的诉状原则;现代审判实践,包括地点、程序、陪审团、证据充分性、指示、判决、新审判、判决;上诉程序。Aldave 女士、Louisell 先生、Poche 先生、Stolz 先生、Vetter 先生

201A-201B。合同。(4) 合同法,处理成立、运作和终止的问题。艾森伯格先生、凯斯勒先生、劳贝先生、温特劳布先生

然后名单还在继续,还有一千多个。

我想分解这些列表的不同部分,并将它们放在每个列表中。例如,我想要一个列表中的所有班级编号、列表中的所有班级名称、列表中的所有单元、列表中的所有班级描述以及列表中的所有教师。

我该怎么办?我刚开始用 php 编码,有什么推荐的阅读材料吗?谢谢你。

4

2 回答 2

1

这符合您的需求吗?(我没有使用点作为分隔符,而是使用#)

$strings = array();

$class_codes = array();
$class_names = array();
$class_units = array();
$class_descriptions = array();
$class_teachers = array();

$strings[] = "200A-200B#Civil Procedure#(3)#The principles of pleading under the code system and the federal rules; modern trial practice, including venue, process, the jury, sufficiency of evidence, instructions, verdicts, new trials, judgments; appellate procedure.#Ms. Aldave, Mr. Louisell, Mr. Poche, Mr. Stolz, Mr. Vetter";
$strings[] = "201A-201B#Contracts#(4)#The law of contracts, dealing with the problems of formation, operation, and termination.#Mr. Eisenberg, Mr. Kessler, Mr. Laube, Mr. Weintraub";

$total = count($strings);

for($i=0; $i<$total; $i++)
{
    $string_parts = explode("#", $strings[$i]);

    $class_codes[] = $string_parts[0];
    $class_names[] = $string_parts[1];
    $class_units[] = $string_parts[2];
    $class_descriptions[] = $string_parts[3];
    $class_teachers[] = $string_parts[4];
}

echo "<pre>";
print_r($class_codes);
echo "</pre>";
于 2013-03-01T01:34:08.560 回答
0

您可以遍历文件的每一行并将正则表达式应用于每一行以获得

//I am just constructing $lines array assuming you have all lines of the file 

$lines[0] = "200A-200B. Civil Procedure. (3) The principles of pleading under the code system and the federal rules;
modern trial practice, including venue, process, the jury, sufficiency of evidence, instructions, verdicts, new trials, judgments; appellate procedure. Ms. Aldave, Mr. Louisell, Mr. Poche, Mr. Stolz, Mr. Vetter";
$lines[1] = "201A-201B. Contracts. (4) The law of contracts, dealing with the prob¬lems of formation, operation, and termination. Mr. Eisenberg, Mr. Kessler, Mr. Laube, Mr. Weintraub";

$regex = '/(.*)\.\s*(.*)\.\s*\(([1-9]+)\)\s*([^\..]*)\.\s*(.*)\s*$/';
$data = array();
foreach($lines as $line)
{
    preg_match($regex, $line, $matches);
    if(isset($matches[1]) &&
       isset($matches[2]) &&
       isset($matches[3]) &&
       isset($matches[4]) &&
       isset($matches[5])
    )
    $data[] = array("class_code" => $matches[1],
                "class_name" => $matches[2],
                "class_unit" => $matches[3],
                "class_description" => $matches[4],
                "class_instructors" => $matches[5]
            );

}

如果您 var_dump 以上$data变量,您将获得以下输出:

array
  0 => 
    array
      'class_code' => string '200A-200B' (length=9)
      'class_name' => string 'Civil Procedure' (length=15)
      'class_unit' => string '3' (length=1)
      'class_description' => string 'The principles of pleading under the code system and the federal rules;
modern trial practice, including venue, process, the jury, sufficiency of evidence, instructions, verdicts, new trials, judgments; appellate procedure' (length=222)
      'class_instructors' => string 'Ms. Aldave, Mr. Louisell, Mr. Poche, Mr. Stolz, Mr. Vetter' (length=58)
  1 => 
    array
      'class_code' => string '201A-201B' (length=9)
      'class_name' => string 'Contracts' (length=9)
      'class_unit' => string '4' (length=1)
      'class_description' => string 'The law of contracts, dealing with the prob¬lems of formation, operation, and termination' (length=90)
      'class_instructors' => string 'Mr. Eisenberg, Mr. Kessler, Mr. Laube, Mr. Weintraub' (length=52)

我希望这就是你要找的..

于 2013-03-01T02:00:14.623 回答