Hi everyone! I have a problem with a very simple solution I've just been unable to find :D 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:


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:

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:

$listtext = ob_get_contents();
preg_match_all("/(Patient|INFORMATION)(.*)[Phone:(H) ](.*)[Birth]/", $listtext, $array);
echo "<br><br>";
echo $listtext;

To no avail. Any help would be greatly appreciated :D 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?

Certainly :D 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 :D.

Please help thank you :D!

In the file you are reading the data from, is each person on their own line?


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

One continuous line. Thank you!

Thats going to make it substantially harder to parse through. Is there some kind of unique character that separates the different individuals?

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.

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!

Okay, here goes:

$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
echo "<br><br>";
echo $listtext;

Oooh why thank you. I just have some issues with it as an address may have the word "North" or "South", making it an extra word/field. Here's a sample of what I have:

Doe, John Phone:(H) (555)555-5555 Birth:01/01/2001 150 Smith Street (W) ( ) SS:555-55-5555 Apt.555 (O) ( ) Chart:555555 Anytown, NY 55555 Fax:( ) DL#: E-Mail: Pager:( ) Other ID: (P)GDE DIS1Billing Type:1 Gender:Male (S) Position:Married Status:Patient

That's one listing ;). I have this in a neater form when I go to Print Preview in the program that has the data. I also have it in a neater form in which it is split into columns and has information going down and across. Thank you.

Oh dear. Do you have the option of reformatting the info file? Because this looks like a job for xml to me.

Here is description of explode() :


(PHP 4, PHP 5)

explode — Split a string by string
array explode ( string $delimiter , string $string [, int $limit ] )

Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter .


The boundary string.

The input string.

If limit is set, the returned array will contain a maximum of limit elements with the last element containing the rest of string .

If the limit parameter is negative, all components except the last -limit are returned.

Although implode() can, for historical reasons, accept its parameters in either order, explode() cannot. You must ensure that the delimiter argument comes before the string argument.
Return Values

If delimiter is an empty string (""), explode() will return FALSE. If delimiter contains a value that is not contained in string , then explode() will return an array containing string .

Version Description
5.1.0 Support for negative limit s was added
4.0.1 The limit parameter was added

Example #1 explode() examples

// Example 1
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2

// Example 2
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo $user; // foo
echo $pass; // *


Example #2 limit parameter examples
$str = 'one|two|three|four';

// positive limit
print_r(explode('|', $str, 2));

// negative limit (since PHP 5.1)
print_r(explode('|', $str, -1));

The above example will output:

[0] => one
[1] => two|three|four
[0] => one
[1] => two
[2] => three


Note: This function is binary-safe.

See Also

* preg_split()
* str_split()
* str_word_count()
* strtok()
* implode()

fprintf> <echo Last updated: Fri, 12 Dec 2008

add a note add a note User Contributed Notes
Elad Elrom
05-Dec-2008 10:32

// Remove words if more than max allowed character are insert or add a string in case less than min are displayed
// Example: LimitText("The red dog ran out of thefence",15,20,"<br>");

function LimitText($Text,$Min,$Max,$MinAddChar) {
   if (strlen($Text) < $Min) {
       $Limit = $Min-strlen($Text);
       $Text .= $MinAddChar;
   elseif (strlen($Text) >= $Max) {
       $words = explode(" ", $Text);
       while (strlen($Text) >= $Max) {
   return $Text;

17-Nov-2008 07:08
A really better and shorter way to get extension is via: <?php $extension = end(explode('.', $filename)); ?> this will print the last part after the last dot :)
30-Aug-2008 12:54
For anyone trying to get an array of key => value pairs from a query string, use parse_str. (Better alternative than the explode_assoc function listed way down the page unless you need different separators.)
pinkgothic at gmail dot com
15-Oct-2007 02:56
coroa at cosmo-genics dot com mentioned using preg_split() instead of explode() when you have multiple delimiters in your text and don't want your result array cluttered with empty elements. While that certainly works, it means you need to know your way around regular expressions... and, as it turns out, it is slower than its alternative. Specifically, you can cut execution time roughly in half if you use array_filter(explode(...)) instead.

Benchmarks (using 'too many spaces'):
Looped 100000 times:
preg_split: 1.61789011955 seconds
filter-explode: 0.916578054428 seconds

Looped 10000 times:
preg_split: 0.162719011307 seconds
filter-explode: 0.0918920040131 seconds

(The relation is, evidently, pretty linear.)

Note: Adding array_values() to the filter-explode combination, to avoid having those oft-feared 'holes' in your array, doesn't remove the benefit, either. (For scale - the '9' becomes a '11' in the benchmarks above.)

Also note: I haven't tested anything other than the example with spaces - since djogo_curl at yahoo's note seems to imply that explode() might get slow with longer delimiters, I expect this would be the case here, too.

I hope this helps someone. :)
seventoes at gmail dot com
10-Dec-2006 09:19
Note that explode, split, and functions like it, can accept more than a single character for the delimiter.

$string = "Something--next--something else--next--one more";


djogo_curl at yahoo
01-Dec-2004 06:20
Being a beginner in php but not so in Perl, I was used to split() instead of explode(). But as split() works with regexps it turned out to be much slower than explode(), when working with single characters.
coroa at cosmo-genics dot com
16-Nov-2003 09:31
To split a string containing multiple seperators between elements rather use preg_split than explode:

preg_split ("/\s+/", "Here are to many spaces in between");

which gives you
array ("Here", "are", "to", "many", "spaces", "in", "between");

For more examples you can visit the link below:


I've now simplified my document to this:

LastName, FirstName Phone(H):(555)555-5555

LastName, FirstName Phone(H):(555)555-5555

And just need a simple regex for that :D Thank you!

Solved thank you all!

Sorry about my incorrect usage of explode in a previous code example, its been a while since i've actually written PHP. (I've got java on the brain)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.