• 0

[PHP] Get certain keywords from a text file based on white-spaces


Question

So after several Advil's I think I need help

I am trying to make a script that lets the user upload a .txt file, the file will look like this as an example

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

Ok, so based off of this what I want to do is this:

The PHP script looks at the file and counts 35 white spaces, since all files will have the same layout and never differ in white spaces I chose this as the best way to go.

for every 35 white spaces extract character 36 until the end of line.

Then tally up $character++

so in the end the output would look like

-----------------------------------

It looks like you have 2 characters in your script

Melinda

Commandante Chitt

-----------------------------------

using PHP to select distint names, and use the strtolower() to lower case the strings and ucfirst() to make the first letter upper-case

thats my project,

I'm at the stage where I'm going crazy trying to figure out how to count white-spaces and everything after that white space until the first white-space after the word IS a character name

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

I am not quite sure what you are trying to achieve. But from what I have read I gather that you are trying to pullout the names from the text and that the identifying marker is 35 white spaces before the name. If I am right then this can be achieved by following steps...

- Get the data from the file in one sweep as a string.

- Split the string using a line return followed by 35 white spaces as the divider.

- Collect all the characters from the beginning till you reach line return.

- Modify case as required.

- Store the data collected in a new array.

Link to comment
Share on other sites

  • 0

Couldn't you just use regular expressions to get the names? Something like this might do it;

// $file will be the contents of the script file as a string
$pattern = '/^s\{35}(.)+\n$/';
preg_match_all($pattern, $file, $characters);
print_r($characters);
$numCharacters = count($characters);
// Then use strtolower etc on each element of the $characters array to get the names in the format you want

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.