I was using explode("\n", $string) to split a huge string (~ 0,5 MB). This took about 15 minutes to complete!
Using strtok($string, "\n") (followed by several thousand strtok("\n") of course), the same splitting is done in less than a minute.
explode
説明
array explode ( string delimiter, string string [, int limit] )文字列の配列を返します。この配列の各要素は、 string を文字列 delimiter で区切った部分文字列となります。 limit が指定された場合、返される配列には 最大 limit の要素が含まれ、その最後の要素には string の残りの部分が全て含まれます。
空の文字列 ("") が delimiter として使用された場合、 explode() は FALSE を返します。delimiter に引数 string に含まれていない値が含まれている場合、 explode() は、引数 string を返します。
もし limit パラメータが負の場合、 最後の -limit 個の要素を除く全ての構成要素が返されます。 この特徴は PHP 5.1.0 で追加されました。
歴史的理由により、implode() はいずれのパラメータ順も受け入れることができますが、 explode() はそうできません。 string 引数の前に必ず delimiter 引数がくるように確認する必要があります。
注意: パラメータ limit は、PHP 4.0.1 で追加されました。
注意: この関数はバイナリデータに対応しています。
preg_split(), spliti(), split(), strtok(), implode(). も参照ください。
explode
16-Jan-2007 07:03
17-Dec-2006 02:28
A 'between' function that we've all been waiting for. I am not savvy with regex so I resorted to explode();
<?php
function between($beg, $end, $str) {
$a = explode($beg, $str, 2);
$b = explode($end, $a[1]);
return $beg . $b[0] . $end;
}
echo between('<a>', '</a>', 'fsdfsdfsd<a>fsdfsd<a><a></a>sdfsdfsdf')
//<a>fsdfsd<a><a></a>
?>
10-Dec-2006 12:49
Note that explode, split, and functions like it, can accept more than a single character for the delimiter.
<?php
$string = "Something--next--something else--next--one more";
print_r(explode('--next--',$string));
?>
21-Oct-2006 03:50
// simple function to remove words if more than max allowed words or add a charcter once less than min
// 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;
}
14-Mar-2006 03:20
If you want to split a price (float) into pounds and pence.
or dollors and cents etc etc.
$price = "6.20";
$split = explode(".", $price);
$pound = $split[0]; // piece1
$pence = $split[1]; // piece2
echo "£ $pound . $pence\n";
01-Dec-2004 09:50
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.
24-Aug-2004 05:30
If you split an empty string, you get back a one-element array with 0 as the key and an empty string for the value.
<?php
$str = '';
$foo = explode( ":", $str );
print_r( $foo );
$foo = split( ":", $str );
print_r( $foo );
$foo = preg_split( "/:/", $str );
print_r( $foo );
?>
In all three cases prints
Array
(
[0] =>
)
This of course means that you must explicitly check to see if the value of the first element is blank, or must check to see if the string being split is blank.
17-Nov-2003 01:01
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");