| | |
Preg_Match? Explode? Please Help :D!
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Aug 2007
Posts: 64
Reputation:
Solved Threads: 0
Hi everyone! I have a problem with a very simple solution I've just been unable to find
Say you have a text file that says "My name is jeffc418. My name is kkeith29. My name is =IceBurn=." How do you get it to say just:
jeffc418
kkeith29
=IceBurn=
Would you use some explodes (which I couldn't get to work) or some preg_match_all's?
Here's the problem I have now:
I have a several hundred page list of names and phone numbers, along with some other garbage. It looks like this:
The ... means that there's a bunch of more information that I just don't need I simply need a script that saves the Name and Phone Number to a MySQL table. Here's what I've tried so far:
To no avail. Any help would be greatly appreciated
My main problem is writing the expression (and I've tried many guides and such). Thanks and have a happy holiday!
Say you have a text file that says "My name is jeffc418. My name is kkeith29. My name is =IceBurn=." How do you get it to say just:jeffc418
kkeith29
=IceBurn=
Would you use some explodes (which I couldn't get to work) or some preg_match_all's?
Here's the problem I have now:
I have a several hundred page list of names and phone numbers, along with some other garbage. It looks like this:
PHP Syntax (Toggle Plain Text)
Last Name, First Name Phone:(H) ######## Birth:##/##/#### ...Patient
The ... means that there's a bunch of more information that I just don't need I simply need a script that saves the Name and Phone Number to a MySQL table. Here's what I've tried so far:
PHP Syntax (Toggle Plain Text)
<?php ob_start(); include_once('listtext.txt'); $listtext = ob_get_contents(); ob_end_clean(); preg_match_all("/(Patient|INFORMATION)(.*)[Phone:(H) ](.*)[Birth]/", $listtext, $array); print_r($array); echo "<br><br>"; echo $listtext; ?>
To no avail. Any help would be greatly appreciated
My main problem is writing the expression (and I've tried many guides and such). Thanks and have a happy holiday! ok, i don't think this will be too hard if the text is all in the same format. the way you stated it, i think it is.
is the data on seperate lines or one long string of text? if its on separate lines, you can use the file() function to get the data into an array of lines which will help prevent error when reading with a preg_match regex.
can you post some exact text we will be matching so a good regex can be made the first time?
is the data on seperate lines or one long string of text? if its on separate lines, you can use the file() function to get the data into an array of lines which will help prevent error when reading with a preg_match regex.
can you post some exact text we will be matching so a good regex can be made the first time?
•
•
Join Date: Aug 2007
Posts: 64
Reputation:
Solved Threads: 0
Certainly
I appreciate the help! If I echo the text, it appears as just many, many lines of text. But if I were to copy and paste it into a Word file, it still appears as the many lines of text, only each of the 500+ pages contains only seven entries, after seven entries it has whitespace and then the next page continues, and at the top of each page it says "PATIENT LIST (STANDARD LIST)" (a little heading). That happens because the program I took the information from set up the information as if to print it (had to acquire it by setting up a to-FILE Printer).
So when I echo the text, it's one continuous line, but if I copy and paste it into a Word document, it's one continuous line per page, with 7 entries per page and every page begins with the same heading (the only variable being the page number displayed in the heading).
Thank you so much for your help, and I hope we can resolve this
.
I appreciate the help! If I echo the text, it appears as just many, many lines of text. But if I were to copy and paste it into a Word file, it still appears as the many lines of text, only each of the 500+ pages contains only seven entries, after seven entries it has whitespace and then the next page continues, and at the top of each page it says "PATIENT LIST (STANDARD LIST)" (a little heading). That happens because the program I took the information from set up the information as if to print it (had to acquire it by setting up a to-FILE Printer).So when I echo the text, it's one continuous line, but if I copy and paste it into a Word document, it's one continuous line per page, with 7 entries per page and every page begins with the same heading (the only variable being the page number displayed in the heading).
Thank you so much for your help, and I hope we can resolve this
. Last edited by jeffc418; Dec 21st, 2008 at 5:13 pm.
•
•
Join Date: Jul 2008
Posts: 148
Reputation:
Solved Threads: 25
In the file you are reading the data from, is each person on their own line?
Example:
Last Name, First Name Phone: (H) ######## Birth:##/##/#### ...Patient
Last Name, First Name Phone: (H) ######## Birth:##/##/#### ...Patient
Last Name, First Name Phone: (H) ######## Birth:##/##/#### ...Patient
or do they all wrap together as one single line of continuous text:
Last Name, First Name Phone: (H) ######## Birth:##/##/#### ...Patient Last Name, First Name Phone: (H) ######## Birth:##/##/#### ...Patient Last Name, First Name Phone: (H) ######## Birth:##/##/#### ...Patient Last Name, First Name Phone: (H) ######## Birth:##/##/#### ...Patient
Example:
Last Name, First Name Phone: (H) ######## Birth:##/##/#### ...Patient
Last Name, First Name Phone: (H) ######## Birth:##/##/#### ...Patient
Last Name, First Name Phone: (H) ######## Birth:##/##/#### ...Patient
or do they all wrap together as one single line of continuous text:
Last Name, First Name Phone: (H) ######## Birth:##/##/#### ...Patient Last Name, First Name Phone: (H) ######## Birth:##/##/#### ...Patient Last Name, First Name Phone: (H) ######## Birth:##/##/#### ...Patient Last Name, First Name Phone: (H) ######## Birth:##/##/#### ...Patient
Last edited by mschroeder; Dec 24th, 2008 at 5:01 pm.
Try exploding with spaces. If you know how many fields there are, (with n being the number of fields) you can say every (nth) field is last name, (nth + 1) field is first name, and so on. This does assume that individual fields cannot contain spaces as part of the value. If you need some sample code, I can provide it.
Last edited by death_oclock; Dec 24th, 2008 at 6:02 pm.
Okay, here goes:
php Syntax (Toggle Plain Text)
<?php ob_start(); include_once('listtext.txt'); $listtext = ob_get_contents(); $listtext = explode($listtext); $array = new array(); for($i = 0; $i < count($listtext) / 8; $i++) // replace 8 with the number of fields { $array[$i]["last_name"] = $listtext[$i * 8 + 0]; // put something to remove the comma $array[$i]["first_name"] = $listtext[$i * 8 + 1]; $array[$i]["phone"] = $listtext[$i * 8 + 4]; // skip the "phone" and "(H)" labels $array[$i]["birth"] = $listtext[$i * 8 + 6]; // add similar statements to get the other fields or just skip them } ob_end_clean(); print_r($array); echo "<br><br>"; echo $listtext; ?>
![]() |
Similar Threads
- preg_match validation help please (PHP)
- Open In New Window Php (PHP)
- Email piping to script problem (PHP)
- hello i need help please (PHP)
- Convert a string to an integer (PHP)
- Mailform error (PHP)
- replace address - how to? (PHP)
- PHP Feezing with a function (PHP)
Other Threads in the PHP Forum
- Previous Thread: Urgently need -php script for searching image
- Next Thread: Desktop Icon
| Thread Tools | Search this Thread |
.htaccess alerts apache api archive array autocomplete beginner binary broken cakephp checkbox class cms code convert cron curl database dataentry date display duplicates dynamic echo email emptydisplayvalue error execute explodefunction file files firstoptioninphpdroplist folder form forms function functions google hack href htaccess html htmlspecialchars image include insert ip javasciptvalidation javascript joomla keywords limit link login mail matching menu methods mlm multiple mysql network object oop paypal pdf php problem query radio random recursion recursive redirect remote script search securephp server sessions shot sms source space sql subscription syntax system table tutorial tutorials update upload url validator variable video web youtube






