★ wanayoo — archive 1999 http://www.php.net/manual/function.split.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Regexps
Quick Reference
Regexps
* ereg
* ereg_replace
* eregi
* eregi_replace
* split
* sql_regcase
Manual: split
View the source code for this pageSearch the site



Previous page
 eregi_replace
sql_regcase 
Next page


split

(PHP3 , PHP4 )

split -- split string into array by regular expression

Description

array split (string pattern, 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 regular expression pattern. If limit is set, the returned array will contaion a maximum of limit elements with the last element containing the whole rest of string. If an error occurs, split() returns false.

To get the first five fields from a line from /etc/passwd:

Example 1. split() example

  1 
  2 $passwd_list = split( ":", $passwd_line, 5 );
  3 	  

To parse a date which may be delimited with slashes, dots, or hyphens:

Example 2. split() example

  1 
  2 $date = "04/30/1973";  // Delimiters may be slash, dot, or hyphen
  3 list( $month, $day, $year ) = split( '[/.-]', $date );
  4 echo "Month: $month; Day: $day; Year: $year<br>\n";
  5 	  

Note that pattern is case-sensitive.

Note that if you don't require the power of regular expressions, it is faster to use explode(), which doesn't incur the overhead of the regular expression engine.

Please note that pattern is a regular expression. If you want to split on any of the characters which are considered special by regular expressions, you'll need to escape them first. If you think split() (or any other regex function, for that matter) is doing something weird, please read the file regex.7, included in the regex/ subdirectory of the PHP distribution. It's in manpage format, so you'll want to do something along the lines of man /usr/local/src/regex/regex.7 in order to read it.

See also: explode() and implode().


User Contributed Notes: split


jesusjr@mindless.com
21-Mar-2000 10:49
How would you replicate the following Perl code in PHP: @letters = split('',$var);


dpk@dpk.net
22-Mar-2000 07:07
$array = split (...) works. Then you can access the array with $array[0]..$array[n] - dpk


kietscia@yahoo.com
22-Mar-2000 09:49
Here's a little unexpected gem. <?php $x = split(":",""); print "There are " . count($x) . " items"; ? Suprisingly enough (or maybe not), this prints There are 1 items
I would expect this to be 0 since the string is empty.


john@doe.com
23-Mar-2000 12:53
I think for what you're probably trying to do, you should use substr: $string = substr($var,0,10); This will grab the first 10 characters.


jier@quadrent.net
08-Apr-2000 11:02
how could i find a new line in a multilined text area and replace it with
? heres what im using and its returning "Array": $message[0] = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,); $message = split( "\n", $message[0], 10 );


jier@quadrent.net
08-Apr-2000 11:03
how could i find a new line in a multilined text area and replace it with
? heres what im using and its returning "Array": $message[0] = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,); $message = split( "\n", $message[0], 10 );


zorka@zorka.com
10-Apr-2000 05:46
How would you split up a "timestamp" column in a mysql database. Exp: 20000428153433 this is in the format of: ymdhms I would like to format this into: $year $month $day ......


algalvez@dcc.uchile.cl
28-Apr-2000 10:10
How could I wirte a regular expression for one or more "\t". I've tried whit these, but any of them works: split("\t"+, $buffer); split([\t]+, $buffer); split('[\t]'+, $buffer);


dante@hearme.com
30-Apr-2000 07:07
Is it possible to reference the item I did the split on as part of the returned array as well? I want to be able to say:
$text = "abcdefghijkl"; $parts = split("(de|hi|k)", $text); but then, I want the array that comes back to contain the following: $parts[0] = "abc"; $parts[1] = "de"; $parts[2] = "fg"; $parts[3] = "hi"; $parts[4] = "j"; $parts[5] = "k"; $parts[6] = "l"; See? Perl lets me do this, but ...i don't see it happening in PHP...just now. Any help? I'd hate to have to ereg match things one at a time...


Richard Stanford &lt;richards@herald.net&gt;
05-May-2000 07:52
jesusjr asked: --- How would you replicate the following Perl code in PHP: @letters = split('',$var); --- The answer is simple: you don't have to. PHP allows $string[] access already. jier asks: how could i find a new line in a multilined text area and replace it with
? --- You're using the wrong function. What you want can be accomplished with: str_replace("\n", "
", $your_string) --- -Richard


richards@herald.net
05-May-2000 07:55
There should have been a
instead of a newline there. For that matter, there should have been brackets instead of their HTML code around my email address... you get the idea though.


richards@herald.net
05-May-2000 07:56
AARGH! Make that a quote, then a less-than sign, then a B, then an R, then a greater-than sign, then another quote. This HTML wrapper is remarkably agressive sometimes.


 About Notes


Previous page
 eregi_replace
sql_regcase 
Next page



Site Statistics


Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.