spacer
downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

splitispacer spacer eregi
[edit] Last updated: Fri, 15 Mar 2013

view this page in

split

(PHP 4, PHP 5)

splitSplit string into array by regular expression

Description

array split ( string $pattern , string $string [, int $limit = -1 ] )

Splits a string into array by regular expression.

Warning

This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.

Parameters

pattern

Case sensitive 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.

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 whole rest of string.

Return Values

Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the case-sensitive regular expression pattern.

If there are n occurrences of pattern, the returned array will contain n+1 items. For example, if there is no occurrence of pattern, an array with only one element will be returned. Of course, this is also true if string is empty. If an error occurs, split() returns FALSE.

Examples

Example #1 split() example

To split off the first four fields from a line from /etc/passwd:

<?php
list($user$pass$uid$gid$extra) =
    
split(":"$passwd_line5);
?>

Example #2 split() example

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

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

Notes

Note:

As of PHP 5.3.0, the regex extension is deprecated in favor of the PCRE extension. Calling this function will issue an E_DEPRECATED notice. See the list of differences for help on converting to PCRE.

Tip

split() is deprecated as of PHP 5.3.0. preg_split() is the suggested alternative to this function. 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.

Tip

For users looking for a way to emulate Perl's @chars = split('', $str) behaviour, please see the examples for preg_split() or str_split().

See Also

  • preg_split() - Split string by a regular expression
  • spliti() - Split string into array by regular expression case insensitive
  • str_split() - Convert a string to an array
  • explode() - Split a string by string
  • implode() - Join array elements with a string
  • chunk_split() - Split a string into smaller chunks
  • wordwrap() - Wraps a string to a given number of characters



splitispacer spacer eregi
[edit] Last updated: Fri, 15 Mar 2013
 
spacer add a note User Contributed Notes split - [38 notes]
up
down
2
Greg Levine
4 years ago
I kept running into the same issue Chris Tyler experienced with lewis [ at t] hcoms [d dot t] co [d dot t] uk's function before realizing that Chris had come up with a solution. However, that solution was just a little off it seems, unless your CSV only contains one line.

If you simply add another --length; in the place you suggested, then the function will always trim the last two characters on the line. Since the newline character is the last character on the line and the redundant quote (or other enclosure) is the second to last character, this works for the final segment. But when parsing segments that do not include a newline character, you end up trimming the redundant enclosure and the last character before the enclosure.

For example,

"he","she","him","her"\r\n

becomes

[0] => h
[1] => sh
[2] => hi
[3] => her

Since the segment could end with the enclosure (i.e., ") or the enclosure followed by the newline (i.e., "\r\n), you have make sure you are only adding another --length; when the latter is the case. Replacing the code block that you suggested with the following will do the trick.

# Is the last thing a newline?
if( $char == $newline )
{
    # Well then get rid of it
    --$length;
}

# Is the last thing a quote?
if( $trim_quote )
{
    # Well then get rid of it
    --$length;
}

I've tested this only for the purposes of the script I'm working on at this time. So, there could be other bugs I haven't come across, but this seems like the easiest way to eliminate the redundant enclosure.
up
down
1
Chris Tyler
4 years ago
Thank you Dave for your code below.  Here is one change I made to avoid a redundant quote at the end of some lines (at least when I used excel: 

Added another --length;  into the if statement below:

                // Is the last thing a quote?
                if ($trim_quote){
                    // Well then get rid of it
                    --$length;
        // ADD TO FIX extra quote
    --$length;
                }
up
down
1
lewis [ at t] hcoms [d dot t] co [d dot t] uk
4 years ago
Those of you trying to use split for CSV, it won't always work as expected. Instead, try using a simple stack method:

<?php

   
/**
     * Create a 2D array from a CSV string
     *
     * @param mixed $data 2D array
     * @param string $delimiter Field delimiter
     * @param string $enclosure Field enclosure
     * @param string $newline Line seperator
     * @return
     */
   
function parse($data, $delimiter = ',', $enclosure = '"', $newline = "\n"){
       
$pos = $last_pos = -1;
       
$end = strlen($data);
       
$row = 0;
       
$quote_open = false;
       
$trim_quote = false;

       
$return = array();

       
// Create a continuous loop
       
for ($i = -1;; ++$i){
            ++
$pos;
           
// Get the positions
           
$comma_pos = strpos($data, $delimiter, $pos);
           
$quote_pos = strpos($data, $enclosure, $pos);
           
$newline_pos = strpos($data, $newline, $pos);

           
// Which one comes first?
           
$pos = min(($comma_pos === false) ? $end : $comma_pos, ($quote_pos === false) ? $end : $quote_pos, ($newline_pos === false) ? $end : $newline_pos);

           
// Cache it
           
$char = (isset($data[$pos])) ? $data[$pos] : null;
           
$done = ($pos == $end);

           
// It it a special character?
           
if ($done || $char == $delimiter || $char == $newline){

               
// Ignore it as we're still in a quote
               
if ($quote_open && !$done){
                    continue;
                }

               
$length = $pos - ++$last_pos;

               
// Is the last thing a quote?
               
if ($trim_quote){
                   
// Well then get rid of it
                   
--$length;
                }

               
// Get all the contents of this column
               
$return[$row][] = ($length > 0) ? str_replace($enclosure . $enclosure, $enclosure, substr($data, $last_pos, $length)) : '';

               
// And we're done
               
if ($done){
                    break;
                }

               
// Save the last position
               
$last_pos = $pos;

               
// Next row?
               
if ($char == $newline){
                    ++
$row;
                }

               
$trim_quote = false;
            }
           
// Our quote?
           
else if ($char == $enclosure){

               
// Toggle it
               
if ($quote_open == false){
                   
// It's an opening quote
                   
$quote_open = true;
                   
$trim_quote = false;

                   
// Trim this opening quote?
                   
if ($last_pos + 1 == $pos){
                        ++
$last_pos;
                    }

                }
                else {
                   
// It's a closing quote
                   
$quote_open = false;

                   
// Trim the last quote?
                   
$trim_quote = true;
                }

            }

        }

        return
$return;
    }

?>

This *should* work for any valid CSV string, regardless of what it contains inside its quotes (using RFC 4180). It should also be faster than most of the others I've seen. It's very simple in concept, and thoroughly commented.
up
down
1
Dave Walter
5 years ago
In response to  the getCSVValues() function posted by justin at cam dot org, my testing indicates that it has a problem with a CSV string like this:

1,2,"3,""4,5"",6",7

This parses as:

array(6) {
  [0]=>
  string(1) "1"
  [1]=>
  string(1) "2"
  [2]=>
  string(4) "3,"4"
  [3]=>
  string(1) "5"
  [4]=>
  string(0) ""
  [5]=>
  string(1) "7"
}

instead of:

array(4) {
  [0]=>
  string(1) "1"
  [1]=>
  string(1) "2"
  [2]=>
  string(9) "3,"4,5",6"
  [3]=>
  string(1) "7"
}

To fix this, I changed the second substr_count to look for an odd number of quotes, as opposed to any quotes at all:

<?php
function getCSVValues($string, $separator=",")
{
   
$elements = explode($separator, $string);
    for (
$i = 0; $i < count($elements); $i++) {
       
$nquotes = substr_count($elements[$i], '"');
        if (
$nquotes %2 == 1) {
            for (
$j = $i+1; $j < count($elements); $j++) {
                if (
substr_count($elements[$j], '"') %2 == 1) { // Look for an odd-number of quotes
                    // Put the quoted string's pieces back together again
                   
array_splice($elements, $i, $j-$i+1,
                       
implode($separator, array_slice($elements, $i, $j-$i+1)));
                    break;
                }
            }
        }
        if (
$nquotes > 0) {
           
// Remove first and last quotes, then merge pairs of quotes
           
$qstr =& $elements[$i];
           
$qstr = substr_replace($qstr, '', strpos($qstr, '"'), 1);
           
$qstr = substr_replace($qstr, '', strrpos($qstr, '"'), 1);
           
$qstr = str_replace('""', '"', $qstr);
        }
    }
    return
$elements;
}
?>
up
down
1
Jort
6 years ago
If you are looking for EITHER open square brackets OR close square brackets, then '[[]]' won't work (reasonably expected), but neither will '[\[\]]', nor with any number of escapes. HOWEVER, if your pattern is '[][]' it will work.
up
down
1
RE: gcerretini at technica dot net /UTF8
7 years ago
Original problem:
=================

I've try using split function.

<?php
$ferro
="2&#65533;12";
$valore=split("[&#65533;]",$ferro);
echo
$ferro."<br>";
echo
"p1-".$valore[0]."<br>";
echo
"p2-".$valore[1]."<br>";
echo
"p3-".$valore[2].
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.