Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: explode - Manual
[go: Go Back, main page]

PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net

search for in the

fprintf" width="11" height="7"/> <echo
Last updated: Wed, 01 Nov 2006
view this page in

explode

(PHP 3, PHP 4, PHP 5)

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 で追加されました。

例 1. explode() の例

<?php
// 例 1
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo
$pieces[0]; // piece1
echo $pieces[1]; // piece2

// 例 2
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list(
$user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo
$user; // foo
echo $pass; // *

?>

例 2. limit パラメータの例

<?php
$str
= 'one|two|three|four';

// 正の値を持つ limit
print_r(explode('|', $str, 2));

// 負の値を持つ limit (PHP 5.1 以降)
print_r(explode('|', $str, -1));
?>

上の例の出力は以下となります。

Array
(
    [0] => one
    [1] => two|three|four
)
Array
(
    [0] => one
    [1] => two
    [2] => three
)

注意: この関数はバイナリデータに対応しています。

preg_split(), spliti(), split(), strtok(), implode(). も参照ください。



add a note add a note User Contributed Notes
explode
Alex
16-Jan-2007 07:03
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.
orlandu96 at gmail dot com
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>
?>
seventoes at gmail dot com
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));
?>
Elad Elrom
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;
}
webmaster at saund-web dot com
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 $pound . $pence\n";
djogo_curl at yahoo
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.
ian at illumen dot co dot uk
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.
coroa at cosmo-genics dot com
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");

fprintf" width="11" height="7"/> <echo
Last updated: Wed, 01 Nov 2006
 
 
show source | credits | sitemap | contact | advertising | mirror sites