943,948 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 2288
  • PHP RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Dec 25th, 2008
0

Re: Preg_Match? Explode? Please Help :D!

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:

PHP Syntax (Toggle Plain Text)
  1. 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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
jeffc418 is offline Offline
64 posts
since Aug 2007
Dec 25th, 2008
0

Re: Preg_Match? Explode? Please Help :D!

Oh dear. Do you have the option of reformatting the info file? Because this looks like a job for xml to me.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Dec 26th, 2008
0

Re: Preg_Match? Explode? Please Help :D!

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
php Syntax (Toggle Plain Text)
  1. <?php
  2. // Example 1
  3. $pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
  4. $pieces = explode(" ", $pizza);
  5. echo $pieces[0]; // piece1
  6. echo $pieces[1]; // piece2
  7.  
  8. // Example 2
  9. $data = "foo:*:1023:1000::/home/foo:/bin/sh";
  10. list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
  11. echo $user; // foo
  12. echo $pass; // *
  13.  
  14. ?>
  15.  
  16. Example #2 limit parameter examples
  17. <?php
  18. $str = 'one|two|three|four';
  19.  
  20. // positive limit
  21. print_r(explode('|', $str, 2));
  22.  
  23. // negative limit (since PHP 5.1)
  24. print_r(explode('|', $str, -1));
  25. ?>

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)
  1. <?php
  2. // Remove words if more than max allowed character are insert or add a string in case less than min are displayed
  3. // Example: LimitText("The red dog ran out of thefence",15,20,"<br>");
  4.  
  5. function LimitText($Text,$Min,$Max,$MinAddChar) {
  6. if (strlen($Text) < $Min) {
  7. $Limit = $Min-strlen($Text);
  8. $Text .= $MinAddChar;
  9. }
  10. elseif (strlen($Text) >= $Max) {
  11. $words = explode(" ", $Text);
  12. $check=1;
  13. while (strlen($Text) >= $Max) {
  14. $c=count($words)-$check;
  15. $Text=substr($Text,0,(strlen($words[$c])+1)*(-1));
  16. $check++;
  17. }
  18. }
  19.  
  20. return $Text;
  21. }
  22. ?>
Nobody
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)
  1. <?php
  2. $string = "Something--next--something else--next--one more";
  3.  
  4. print_r(explode('--next--',$string));
  5. ?>
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
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.
Reputation Points: 7
Solved Threads: 1
Light Poster
Jenniferlinn is offline Offline
25 posts
since Dec 2008
Dec 26th, 2008
0

Re: Preg_Match? Explode? Please Help :D!

I've now simplified my document to this:

PHP Syntax (Toggle Plain Text)
  1. LastName, FirstName Phone(H):(555)555-5555
  2.  
  3. LastName, FirstName Phone(H):(555)555-5555

And just need a simple regex for that Thank you!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
jeffc418 is offline Offline
64 posts
since Aug 2007
Dec 26th, 2008
0

Re: Preg_Match? Explode? Please Help :D!

Solved thank you all!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
jeffc418 is offline Offline
64 posts
since Aug 2007
Dec 26th, 2008
0

Re: Preg_Match? Explode? Please Help :D!

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)
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Urgently need -php script for searching image
Next Thread in PHP Forum Timeline: Desktop Icon





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC