470,874 Members | 1,789 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,874 developers. It's quick & easy.

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("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
Jul 17 '05 #1
3 1810
William Holroyd wrote:
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-]*"
// 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>';
?>


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 :
Jul 17 '05 #2
William Holroyd a écrit le 07/05/2004 :
Also, if you know how to be able to READ a PDF file in PHP let me know.


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
Jul 17 '05 #3
In article <c7**********@cnn.cns.ksu.edu>,
"William Holroyd" <w.*******@versity-cobalt.com> wrote:
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.


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

JPK

--
Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by supercomputer | last post: by
4 posts views Thread by ralphNOSPAM | last post: by
17 posts views Thread by Mark | last post: by
27 posts views Thread by SQL Learner | last post: by
2 posts views Thread by RG | last post: by
6 posts views Thread by korovev76 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.