开发者

split a line into several array elements

开发者 https://www.devze.com 2023-02-11 15:29 出处:网络
I just got help from some on this site and was able to let a user upload a text file, and have the in the upload process grab the file and programatically search for keywords I specify. The script the

I just got help from some on this site and was able to let a user upload a text file, and have the in the upload process grab the file and programatically search for keywords I specify. The script then counts how many times the word is found and outputs the entire line that it was found in into an array.

So the example results returns this when using this 开发者_StackOverflow社区code:

$sceneINT = $sf->countKeyWord('INT', $file);

with my class looking like so:

public static function countKeyWord($word, $file){
    if(!$word)
        return NULL;

    $contents = file_get_contents($file);

    #standardise those line endings
    $contents = str_replace(array("\r","\r\n","\n\r","\n"),"\n",$contents);
    $lines = explode("\n", $contents);

    #find your result
    $result = $line_num = array();
    foreach($lines as $line_num => $l)
        if(strpos($l, $word)) {
            $result[] = $l;
            $line_nums[] = $line_num;
        }

    echo "<pre>"; // I am echoing out the results for debugging purpuses
    print_r($result);
    echo "</pre>";
    return count($result); //final result shown to the user will only be the count
}

The results from this look like this:

Array
(
    [0] =>   3     INT. MARTEY'S OFFICE - DAY                                      3
    [1] =>   4     INT. RONNEY'S OFFICE - DAY                                      4
    [2] =>   6     INT. - BREEZE'S APARTMENT - DAY                                 6
    [3] =>   9     INT. - WAREHOUSE/SOUNDSTAGE - DAY                               9
    [4] =>  11     INT. EXAM ROOM - DAY                                           11
    [5] =>  12     INT. RAJA'S OFFICE - LATER                                     12
    [6] =>  14     INT. RAJA'S OFFICE - LATER                                     14
    [7] =>  15     INT. LARGE OPERATING ROOM - DAY                                15
    [8] =>  16     INT. RAJA'S OFFICE - LATER                                     16
    [9] =>  17     INT. OLIVER'S CAR - DAY                                        17
    [10] =>  20     INT. - ROY THUNDER'S OFFICE - NIGHT                            20
    [11] =>  22     A 2ND CLIP FROM "GOLDEN GATE GUNS"- INT. BASEMENT - DAY        22
    [12] =>  27     INT. HOUSE WIFE #3'S HOUSE - LATER                             27
    [13] =>  29     INT.  LIBRARY - DAY                                            29
    [14] =>  31     INT.  COFFEE SHOP - NIGHT                                      31
    [15] =>  32     INT. WAITING AREA - DAY                                        32
    [16] =>  33     INT. CASTING OFFICE - DAY                                      33
    [17] =>  34     INT. CASTING OFFICE - DAY                                      34
    [18] =>  35     INT. WAITING AREA - DAY                                        35
    [19] =>  36     INT. WAITING AREA - LATER                                      36
    [20] =>  37     INT. MOTEL ROOM - DAY                                          37
    [21] =>         INT. WAITING AREA - LATER
    [22] =>  39     INT. WAITING AREA - DAY                                        39
    [23] =>  42     INT. WAITING AREA - DAY                                        42
    [24] =>  43     INT. CASTING OFFICE - DAY                                      43
    [25] =>  44     INT. AUDITION ROOM - DAY                                       44
    [26] =>         INT. AUDITION ROOM - DAY
    [27] =>  45     INT. WAITING AREA - DAY                                        45
    [28] =>  46     INT. CASTING OFFICE - DAY                                      46
    [29] =>  47     INT. AUDITION ROOM - DAY                                       47
    [30] =>  48     INT. CASTING OFFICE - DAY                                      48
    [31] =>  49     INT. WAITING AREA - DAY                                        49
    [32] =>  50     INT. AUDITION ROOM - DAY                                       50
    [33] =>  51     INT. CASTING OFFICE - DAY                                      51
    [34] =>  52     INT. WAITING AREA - DAY                                        52
    [35] =>  53     INT. CASTING OFFICE - DAY                                      53
    [36] =>  54     INT. BURGER JOINT  - NIGHT                                     54
)

I need to upload the results into a database;

Taking Array[0] for example I would need to prepare that line for the database to look like this

scene: 3
int_ext: INT
scene_name:    MARTEY'S OFFICE
day_night:    DAY

All that will go into 1 row in the database, I don't know how to approach this. How can I take the result and split into what I need and then send it to the database so that ALL items found are stored when the user presses the SAVE button.


Your code has some errors.

  • You create array $line_num but use $line_nums to add entries
  • You don't use $line_nums, why do you create it?

I would use a regular expression to find all relevant information:

$contents = file_get_contents($file);
$pattern = "!INT\. (.*?) - (MORNING|NIGHT|DAY|LATER)!si";
preg_match_all($pattern, $contents, $matches);
echo '<pre>';
print_r($matches);
echo '</pre>';

.

If you really need the line number, you have to go another way:

Use preg_replace to replace all line breaks:

//OLD: $contents = str_replace(array("\r","\r\n","\n\r","\n"),"\n",$contents);
//replaces even empty rows
$contents = preg_replace("!(\r|\n|\r\n\r\n|\r\r|\n\n)!s", "\n", $contents);

You can use the the regexp of m4rc to split your data ;-)


Heres a really really quick regex I just did. I'm sure that you can tidy it up a bit but it gives you something to go on. I'd look up http://php.net/manual/en/function.preg-split.php

foreach($result as $line)
{
    $splitLine = preg_split("/(\d{1,3})[ ]+([A-Z.]{4}) ([A-Z' ]{1,}) - ([A-Z]{3,})/", $line);
}

That should split it up into an array. I've tested it with the first line of your array. one thing to be aware of is that it wasn't clear whether there were any padding spaces before the first number on each line of the array - so you might have to play around with the regex.

0

精彩评论

暂无评论...
验证码 换一张
取 消