Connecting Tech Pros Worldwide Forums | Help | Site Map

Parsing data from a string into an array for each match

William Holroyd
Guest
 
Posts: n/a
#1: Jul 17 '05
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("classes.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
Guest
 
Posts: n/a
#2: Jul 17 '05

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("classes.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
http://www.php.net/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 :
Jedi121
Guest
 
Posts: n/a
#3: Jul 17 '05

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) :
http://www.pdflib.com/products/pdflib/pdi.html

FDPDF project (kind of PDFLib for free) :
http://fpdf.org/?lang=en

Tutorial for FPDF :
http://www.zend.com/zend/tut/tutorial-PDFgen1.php


Jan Pieter Kunst
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Parsing data from a string into an array for each match


In article <c7ehta$9qa$1@cnn.cns.ksu.edu>,
"William Holroyd" <w.holroyd@versity-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" = @.
Closed Thread