php如何从文本文件中获取某些关键字?

时间:2011-01-13 02:01:46

标签: php

所以在几个Advil之后,我想我需要帮助

我正在尝试创建一个允许用户上传.txt文件的脚本,该文件将如下所示

           EXT. DUNKIN' DONUTS - DAY

           Police vehicles remain in the parking lot.  The determined
           female reporter from the courthouse steps, MELINDA FUENTES
           (32), interviews Comandante Chitt, who holds a napkin to his
           jaw, like he cut himself shaving.

                               MELINDA
                     < Comandante Chitt, how does it
                     feel to get shot in the face? >

                               COMANDANTE CHITT
                     < Not too different than getting
                     shot in the arm or leg. >

                               MELINDA
                     < Tell us what happened. >

                               COMANDANTE CHITT
                     < I parked my car.
                         (indicates assault vehicle
                          in donut shop)
                     He aimed his weapon at my head.  I
                     fired seven shots. He stopped
                     aiming his weapon at my head. >

           Melinda waits for more, but Chitt turns and walks away into
           the roped-off crime scene. Melinda is confused for a second,
           then resumes smiling.

                               MELINDA
                     < And there you have it... A man of
                     few words. >

好的,基于此,我想做的是:

PHP脚本查看文件并计算35个空格,因为所有文件都具有相同的布局,并且在空白区域中从不相同我选择此作为最佳方式。

每35个空白区域提取字符36直到行尾。

然后计算$ character ++

所以最后输出看起来像


您的脚本中似乎有2个字符

梅林达

Commandante Chitt

使用PHP选择distint名称,并使用strtolower()小写字符串和ucfirst()使第一个字母大写

那是我的项目,

我正处于疯狂的阶段,试图弄清楚如何计算白色空间和白色空间之后的所有内容,直到单词后面的第一个空白字符是一个字符名称

2 个答案:

答案 0 :(得分:0)

使用此

substr_count($text, ' ');

所以

<?php
$dog = 0;
foreach($file as $text){
  $number = substr_count($text, ' ');
if($number == 15){
  $dog++;
  //You can do things like save all of the dogs into an array here or similar
}
echo "You have: ".$dog." dogs in this file";
?>

答案 1 :(得分:0)

嗯,你可以分开:

$line = "+++++++++++++++cat:+Garfield";
$chars = split("", $line)

$i = 0;
while( $i < $chars.length && $chars[$i] == '+' ) $i++;
$i--;

switch($i)
  Case 7:
     $person = Substr($line, 7, ($chars.length - 7);
     break;

  Case 15: 
    $cat = Substr($line, 20, ($chars.length - 20);
    break;

等等......你可以使用这样的东西