| | |
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
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:
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.
PHP Syntax (Toggle Plain Text)
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. Last edited by jeffc418; Dec 25th, 2008 at 2:17 pm.
•
•
Join Date: Dec 2008
Posts: 25
Reputation:
Solved Threads: 1
Here is description of explode() :
explode
(PHP 4, PHP 5)
explode — Split a string by string
Description
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 .
Parameters
delimiter
The boundary string.
string
The input string.
limit
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 .
ChangeLog
Version Description
5.1.0 Support for negative limit s was added
4.0.1 The limit parameter was added
Examples
Example #1 explode() examples
The above example will output:
Array
(
[0] => one
[1] => two|three|four
)
Array
(
[0] => one
[1] => two
[2] => three
)
Notes
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
explode
Elad Elrom
05-Dec-2008 10:32
Nobody
17-Nov-2008 07:08
A really better and shorter way to get extension is via:
this will print the last part after the last dot
shaun
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.
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:
http://www.w3schools.com/PHP/func_string_explode.asp
explode
(PHP 4, PHP 5)
explode — Split a string by string
Description
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 .
Parameters
delimiter
The boundary string.
string
The input string.
limit
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 .
ChangeLog
Version Description
5.1.0 Support for negative limit s was added
4.0.1 The limit parameter was added
Examples
Example #1 explode() examples
php Syntax (Toggle Plain Text)
<?php // 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 <?php $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:
Array
(
[0] => one
[1] => two|three|four
)
Array
(
[0] => one
[1] => two
[2] => three
)
Notes
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
explode
Elad Elrom
05-Dec-2008 10:32
php Syntax (Toggle Plain Text)
<?php // 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); $check=1; while (strlen($Text) >= $Max) { $c=count($words)-$check; $Text=substr($Text,0,(strlen($words[$c])+1)*(-1)); $check++; } } 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

shaun
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.
php Syntax (Toggle Plain Text)
<?php $string = "Something--next--something else--next--one more"; print_r(explode('--next--',$string)); ?>
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:
http://www.w3schools.com/PHP/func_string_explode.asp
Last edited by peter_budo; Dec 29th, 2008 at 5:43 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
•
•
Join Date: Aug 2007
Posts: 64
Reputation:
Solved Threads: 0
I've now simplified my document to this:
And just need a simple regex for that
Thank you!
PHP Syntax (Toggle Plain Text)
LastName, FirstName Phone(H):(555)555-5555 LastName, FirstName Phone(H):(555)555-5555
And just need a simple regex for that
Thank you! ![]() |
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 |
ajax apache api array basics beginner binary broken cakephp checkbox class cms code codingproblem combobox cron curl database date display domain dynamic echo email error file files folder form format forms function functions google href htaccess html image include insert interactive ip java javascript joomla js limit link load login mail malfunctioning menu mlm mobile multiple mysql nodes oop outofmemmory paging parse paypal pdf php problem procedure query radio ram random recursion reference remote return script search server sessions sms source space sql syntax system table tutorial unset up-to-date update upload url validation validator variable video web webapplications websitecontactform youtube





