Preg_Match? Explode? Please Help :D!

Reply

Join Date: Aug 2007
Posts: 64
Reputation: jeffc418 is an unknown quantity at this point 
Solved Threads: 0
jeffc418 jeffc418 is offline Offline
Junior Poster in Training

Preg_Match? Explode? Please Help :D!

 
0
  #1
Dec 21st, 2008
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:

  1. 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:

  1. <?php
  2. ob_start();
  3. include_once('listtext.txt');
  4. $listtext = ob_get_contents();
  5. ob_end_clean();
  6. preg_match_all("/(Patient|INFORMATION)(.*)[Phone:(H) ](.*)[Birth]/", $listtext, $array);
  7. print_r($array);
  8. echo "<br><br>";
  9. echo $listtext;
  10. ?>

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!
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: Preg_Match? Explode? Please Help :D!

 
0
  #2
Dec 21st, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 64
Reputation: jeffc418 is an unknown quantity at this point 
Solved Threads: 0
jeffc418 jeffc418 is offline Offline
Junior Poster in Training

Re: Preg_Match? Explode? Please Help :D!

 
0
  #3
Dec 21st, 2008
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 .
Last edited by jeffc418; Dec 21st, 2008 at 5:13 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 64
Reputation: jeffc418 is an unknown quantity at this point 
Solved Threads: 0
jeffc418 jeffc418 is offline Offline
Junior Poster in Training

Re: Preg_Match? Explode? Please Help :D!

 
0
  #4
Dec 24th, 2008
Please help thank you !
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 148
Reputation: mschroeder is on a distinguished road 
Solved Threads: 25
mschroeder mschroeder is offline Offline
Junior Poster

Re: Preg_Match? Explode? Please Help :D!

 
0
  #5
Dec 24th, 2008
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
Last edited by mschroeder; Dec 24th, 2008 at 5:01 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 64
Reputation: jeffc418 is an unknown quantity at this point 
Solved Threads: 0
jeffc418 jeffc418 is offline Offline
Junior Poster in Training

Re: Preg_Match? Explode? Please Help :D!

 
0
  #6
Dec 24th, 2008
One continuous line. Thank you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 148
Reputation: mschroeder is on a distinguished road 
Solved Threads: 25
mschroeder mschroeder is offline Offline
Junior Poster

Re: Preg_Match? Explode? Please Help :D!

 
0
  #7
Dec 24th, 2008
Thats going to make it substantially harder to parse through. Is there some kind of unique character that separates the different individuals?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 353
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: Preg_Match? Explode? Please Help :D!

 
0
  #8
Dec 24th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 64
Reputation: jeffc418 is an unknown quantity at this point 
Solved Threads: 0
jeffc418 jeffc418 is offline Offline
Junior Poster in Training

Re: Preg_Match? Explode? Please Help :D!

 
0
  #9
Dec 25th, 2008
Ahh why that's genius! If you could provide some sample code that would be phenomenal as i'm unsure how to save the variables in the $var[1][2] format systematically. Thank you and Merry Christmas to all!
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 353
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: Preg_Match? Explode? Please Help :D!

 
0
  #10
Dec 25th, 2008
Okay, here goes:
  1. <?php
  2. ob_start();
  3. include_once('listtext.txt');
  4. $listtext = ob_get_contents();
  5. $listtext = explode($listtext);
  6. $array = new array();
  7. for($i = 0; $i < count($listtext) / 8; $i++) // replace 8 with the number of fields
  8. {
  9. $array[$i]["last_name"] = $listtext[$i * 8 + 0]; // put something to remove the comma
  10. $array[$i]["first_name"] = $listtext[$i * 8 + 1];
  11. $array[$i]["phone"] = $listtext[$i * 8 + 4]; // skip the "phone" and "(H)" labels
  12. $array[$i]["birth"] = $listtext[$i * 8 + 6];
  13. // add similar statements to get the other fields or just skip them
  14. }
  15. ob_end_clean();
  16. print_r($array);
  17. echo "<br><br>";
  18. echo $listtext;
  19. ?>
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC