Parsing data from a string into an array for each match

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • William Holroyd

    #1

    Parsing data from a string into an array for each match

    I've had experience with PHP building simple websites and have only
    scratched the surface of what it's capable of, so in a sense I'm a newbie.
    However the problem I have is that I want to be able to read a file, store
    it into a string, and be able to look for patterns in it to extract. I know
    what the regex expression is for getting the data, but not familiar with how
    to read the string and for each match, store it in an array. I haven't been
    able to find any sample code and my php geek buddy is on vacation.

    <?php

    $string = implode(file("c lasses.txt"));
    $string = ereg_replace ("/r|/n", "", $string);

    // this is where I have no clue

    ?>

    Regex expression going to be used - "[A-Z]{3,4} [0-9.]{4} [a-zA-Z.
    \n\r]*\(([0-9]|Var.)\) [a-zA-Z;,. \n\r-]*"

    Also, if you know how to be able to READ a PDF file in PHP let me know.
    Cutting and pasting twenty 6.1MB PDFs will get annoying. I'm just trying to
    make a script to save me time in the future.

    William Holroyd


  • Pedro Graca

    #2
    Re: Parsing data from a string into an array for each match

    William Holroyd wrote:[color=blue]
    > I've had experience with PHP building simple websites and have only
    > scratched the surface of what it's capable of, so in a sense I'm a newbie.
    > However the problem I have is that I want to be able to read a file, store
    > it into a string, and be able to look for patterns in it to extract. I know
    > what the regex expression is for getting the data, but not familiar with how
    > to read the string and for each match, store it in an array. I haven't been
    > able to find any sample code and my php geek buddy is on vacation.
    >
    > <?php
    >
    > $string = implode(file("c lasses.txt"));
    > $string = ereg_replace ("/r|/n", "", $string);
    >
    > // this is where I have no clue
    >
    > Regex expression going to be used - "[A-Z]{3,4} [0-9.]{4} [a-zA-Z.
    > \n\r]*\(([0-9]|Var.)\) [a-zA-Z;,. \n\r-]*"[/color]

    // I didn't look at the regex, just simply copied it here
    $regex = "[A-Z]{3,4} [0-9.]{4} [a-zA-Z.\n\r]*\(([0-9]|Var.)\) [a-zA-Z;,. \n\r-]*";


    $num = preg_match_all( "/$regex/", $string, $found_array);


    if ($num === false) exit('Error in preg_match_all( ).');
    echo '<pre>Found ', $num, ' matches: ';
    print_r($Found_ array);
    echo '</pre>';
    [color=blue]
    > ?>[/color]

    Check the fine manual for preg_match_all


    --
    USENET would be a better place if everybody read: : mail address :
    http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
    http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
    http://www.expita.com/nomime.html : to 10K bytes :

    Comment

    • Jedi121

      #3
      Re: Parsing data from a string into an array for each match

      William Holroyd a écrit le 07/05/2004 :[color=blue]
      > Also, if you know how to be able to READ a PDF file in PHP let me know.[/color]

      There's some solution to create but few to actually read PDF.
      Maybe you could find enough material in this two famous sites.

      An extension to the PDFLib (not free sorry) :


      FDPDF project (kind of PDFLib for free) :


      Tutorial for FPDF :



      Comment

      • Jan Pieter Kunst

        #4
        Re: Parsing data from a string into an array for each match

        In article <c7ehta$9qa$1@c nn.cns.ksu.edu> ,
        "William Holroyd" <w.holroyd@vers ity-cobalt.com> wrote:
        [color=blue]
        > However the problem I have is that I want to be able to read a file, store
        > it into a string, and be able to look for patterns in it to extract. I know
        > what the regex expression is for getting the data, but not familiar with how
        > to read the string and for each match, store it in an array.[/color]

        <http://nl.php.net/preg_match>
        <http://nl.php.net/preg_match_all>

        JPK

        --
        Sorry, <devnull@cauce. org> is een "spam trap".
        E-mail adres is <jpk"at"akamail .com>, waarbij "at" = @.

        Comment

        Working...