开发者

is it possible to extract certain strings based off a predefined white-space count?

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
                     开发者_JS百科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 distinct 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


<html><body><pre><?php
        $story = "           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. >";
$_ = null;
if (preg_match_all('/\s{35}(.*)/m',$story,$_))
{
  $unique_characters = array_unique(array_map(create_function('$a','return ucfirst(strtolower(trim($a)));'),$_[1]));

  echo "-------------------------\r\n";
  echo "It looks like you have ".count($unique_characters)." characters in your script\r\n";
  echo "\r\n";
  array_map(create_function('$a','echo "{$a}\r\n";'),$unique_characters);
  echo "-------------------------";
}
?></pre></body></html>

You will notice $_[1] returns all the characters names found. Then, you can use array_unique to find only the new characters (and display them as you have done).

EDIT
Added an update that does the work as requested. Also a working example (feel free to copy-paste to your own file and run it). Also-also, please excuse my short-hand and use of array_map, create_function, etc. ;p

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜