|
|
split (PHP 3, PHP 4, PHP 5) split -- Split string into array by regular expression Descriptionarray split ( string pattern, string string [, int limit] ) Подсказка:
preg_split(), which uses a Perl-compatible regular
expression syntax, is often a faster alternative to
split(). 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.
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 limit
is set, the returned array will contain a maximum of
limit elements with the last element
containing the whole rest of string. If
an error occurs, split() returns FALSE.
To split off the first four fields from a line from
/etc/passwd:
Пример 1. split() example |
<?php
list($user, $pass, $uid, $gid, $extra) =
split(":", $passwd_line, 5);
?>
|
|
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.
To parse a date which may be delimited with slashes, dots, or
hyphens:
Пример 2. split() example |
<?php
$date = "04/30/1973";
list($month, $day, $year) = split('[/.-]', $date);
echo "Month: $month; Day: $day; Year: $year<br />\n";
?>
|
|
For users looking for a way to emulate Perl's @chars =
split('', $str) behaviour, please see the examples for
preg_split() or str_split().
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: preg_split(), spliti(),
str_split(),
explode(), implode(),
chunk_split(), and wordwrap().
Nate Sweet
31-May-2007 01:39
This function takes a string like this...
this=cool that="super cool" thing='ridiculous cool'
And gives you an associative array of names and values.
function parseNameValues ($text) {
$values = array();
if (preg_match_all('/([^=\s]+)=("(?P<value1>[^"]+)"|' . '\'(?P<value2>[^\']+)\'|(?P<value3>.+?)\b)/', $text, $matches, PREG_SET_ORDER))
foreach ($matches as $match)
$values[trim($match[1])] = trim(@$match['value1'] . @$match['value2'] . @$match['value3']);
return $values;
}
(regex broken into two strings so it won't be too long for this webpage)
Mike
18-May-2007 10:40
// Split a string into words on boundaries of one or more spaces, tabs or new-lines
$s = "Please cut \t me \n in pieces";
$words = split("[\n\r\t ]+", $s);
print_r($words);
// Output:
Array
(
[0] => Please
[1] => cut
[2] => me
[3] => in
[4] => pieces
)
Jort
14-Mar-2007 01:53
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.
justin at cam dot org
15-Feb-2007 10:21
The previous solution assumes that a quoted string always starts a new element (true in real CSV files, but not in my application). The following routine does not make any such assumptions. It also deals with pairs of quotes and does not use any regular expressions.
<?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], '"') > 0) {
array_splice($elements, $i, $j-$i+1,
implode($separator, array_slice($elements, $i, $j-$i+1)));
break;
}
}
}
if ($nquotes > 0) {
$qstr =& $elements[$i];
$qstr = substr_replace($qstr, '', strpos($qstr, '"'), 1);
$qstr = substr_replace($qstr, '', strrpos($qstr, '"'), 1);
$qstr = str_replace('""', '"', $qstr);
}
}
return $elements;
}
?>
Theodule
24-Jan-2007 08:41
A little modification from dan jones.
_New_: I had a parameter to specify separator (default ",").
_Fix _: double-quotes who appear in a spreadsheet exported to CSV, they get escaped by doubling them. So I remplace them by ' caracter
<?php
function getCSVValues($string,$separator=",")
{
$string = str_replace('""', "'", $string);
$bits = explode('"',$string);
$elements = array();
for ( $i=0; $i < count($bits) ; $i++ ) {
if (($i%2) == 1) {
$elements[] = $bits[$i];
} else
{
$rest = $bits[$i];
$rest = preg_replace("/^".$separator."/","",$rest);
$rest = preg_replace("/".$separator."$/","",$rest);
$elements = array_merge($elements,explode($separator,$rest));
}
}
return $elements;
}
?>
dan dot jones at lunarfish dot co dot uk
17-Aug-2006 05:24
Here's a function to split a string into csv values where they are optionally enclosed by " to allow values with commas in.
I think it works. Let me know if I'm wrong.
Cheers. Dan
function getCSVValues($string) {
// split the string at double quotes "
$bits = split('"',$string);
$elements = array();
for ($i=0;$i<count($bits);$i++) {
/*
odd numbered elements would have been
enclosed by double quotes
even numbered elements would not have been
*/
if (($i%2) == 1) {
/* if the element number is odd add the
whole string to the output array */
$elements[] = $bits[$i];
} else {
/* otherwise split the unquoted stuff at commas
and add the elements to the array */
$rest = $bits[$i];
$rest = preg_replace("/^,/","",$rest);
$rest = preg_replace("/,$/","",$rest);
$elements = array_merge($elements,split(',',$rest));
}
}
return $elements;
}
destes at ix dot netcom dot com
14-Feb-2006 12:15
Some corrections to robin-at-teddyb's CSV splitting function. Recall that the point of this is to properly implement a split() function that handles data exported to CSV, where data containing commas gets quote-delimited.
* Problem 1: As jh-at-junetz pointed out, the +1 in robin's nonquoted splitting command mistakenly adds an extra element to the resulting array.
* Problem 2: If consecutive fields are quote-delimited, the remaining "separator" between them only contains one delimiter and no actual fields - so an extra element gets added to the parsed array.
* Problem 3: When double-quotes appear in a spreadsheet exported to CSV, they get escaped by doubling them, i.e. a data field reading "this is a test of a "special" case" gets written to CSV as, "this is a test of a ""special"" case". These quotes are also interpreted as top-level delimiters and (mistakenly) add extra array elements to the output.
I have hacked a conversion of "" to a single quote ( ' ), but a truly clever preg_split for the top-level splitter (instead of the explode) might preserve the original doubled "s without bugging up the top-level parsing. i.e., a smarter man than I could solve the problem rather than avoiding it by replacing the bad data.
(current) Solution:
<?php
function quotesplit( $splitter=',', $s, $restore_quotes=0 ) {
$s = str_replace('""', "'", $s);
$getstrings = explode('"', $splitter.$s.$splitter);
$delimlen = strlen($splitter);
$instring = 0;
while (list($arg, $val) = each($getstrings)) {
if ($instring==1) {
if( $restore_quotes ) {
$result[count($result)-1] = $result[count($result)-1].'"'.$val.'"';
} else {
$result[] = $val;
}
$instring = 0;
} else {
if ((strlen($val)-$delimlen) >= 1) {
$temparray = split($splitter, substr($val, $delimlen, strlen($val)-$delimlen-$delimlen ) );
while(list($iarg, $ival) = each($temparray)) {
$result[] = trim($ival);
}
}
$instring = 1;
}
}
return $result;
}
?>
RE: gcerretini at technica dot net /UTF8
08-Feb-2006 04:26
Original problem:
=================
I've try using split function.
<?php
$ferro="2�12";
$valore=split("[�]",$ferro);
echo $ferro."<br>";
echo "p1-".$valore[0]."<br>";
echo "p2-".$valore[1]."<br>";
echo "p3-".$valore[2]."<br>";
$ferro="2d12";
$valore=split("[d]",$ferro);
echo $ferro."<br>";
echo "p1-".$valore[0]."<br>";
echo "p2-".$valore[1]."<br>";
echo "p3-".$valore[2]."<br>";
?>
This return:
============
2�12
p1-2
p2-
p3-12
2d12
p1-2
p2-12
p3-
I use charset UTF-8. When I use char � the split function ad an empty string between "2" and "12"... Why?
Explanation:
============
UTF-8 charset codes some characters (like the "�" character) into two bytes. In fact the regular expresion "[�]" contains 4 bytes (4 non-unicode characters). To demonstrate the real situation I wrote following example:
$ferro="2de12";
$valore=split("[de]",$ferro);
echo $ferro."<br>";
echo "p1-".$valore[0]."<br>";
echo "p2-".$valore[1]."<br>";
echo "p3-".$valore[2]."<br>";
This returns:
=============
2d12
p1-2
p2-
p3-12
gcerretini at technica dot net
27-Jan-2006 10:46
I've try using split function.
<?PHP
$ferro="2
|
|