explode

explode

(PHP 4, PHP 5)

explode使用一个字符串分割另一个字符串

说明

array explode ( string $separator , string $string [, int $limit ] )

此函数返回由字符串组成的数组,每个元素都是 string 的一个子串,它们被字符串 separator 作为边界点分割出来。如果设置了 limit 参数,则返回的数组包含最多 limit 个元素,而最后那个元素将包含 string 的剩余部分。

如果 separator 为空字符串(""),explode() 将返回 FALSE。如果 separator 所包含的值在 string 中找不到,那么 explode() 将返回包含 string 单个元素的数组。

如果 limit 参数是负数,则返回除了最后的 -limit 个元素外的所有元素。此特性是 PHP 5.1.0 中新增的。

由于历史原因,虽然 implode() 可以接收两种参数顺序,但是 explode() 不行。你必须保证 separator 参数在 string 参数之前才行。

Note:

参数 limit 是在 PHP 4.0.1 中加入的。

Example #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// *

?>

Example #2 limit 参数例子

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

// 正数的 limit
print_r(explode('|'$str2));

// 负数的 limit(自 PHP 5.1 起)
print_r(explode('|'$str, -1));
?>

以上例程会输出:

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

Note: 此函数可安全用于二进制对象。

参见 preg_split()spliti()split()strtok()implode()


  • addcslashes
  • addslashes
  • bin2hex
  • chop
  • chr
  • chunk_split
  • convert_cyr_string
  • convert_uudecode
  • convert_uuencode
  • count_chars
  • crc32
  • crypt
  • echo
  • explode
  • fprintf
  • get_html_translation_table
  • hebrev
  • hebrevc
  • hex2bin
  • htmlentities
  • htmlspecialchars
  • htmlspecialchars_decode
  • html_entity_decode
  • implode
  • join
  • levenshtein
  • localeconv
  • ltrim
  • md5
  • md5_file
  • metaphone
  • money_format
  • nl2br
  • nl_langinfo
  • number_format
  • ord
  • parse_str
  • print
  • printf
  • quoted_printable_decode
  • quoted_printable_encode
  • quotemeta
  • rtrim
  • setlocale
  • sha1
  • sha1_file
  • similar_text
  • soundex
  • sprintf
  • sscanf
  • strcasecmp
  • strchr
  • strcmp
  • strcoll
  • strcspn
  • stripcslashes
  • stripos
  • stripslashes
  • strip_tags
  • stristr
  • strlen
  • strnatcasecmp
  • strnatcmp
  • strncasecmp
  • strncmp
  • strpbrk
  • strpos
  • strrchr
  • strrev
  • strripos
  • strrpos
  • strspn
  • strstr
  • strtok
  • strtolower
  • strtoupper
  • strtr
  • str_getcsv
  • str_ireplace
  • str_pad
  • str_repeat
  • str_replace
  • str_rot13
  • str_shuffle
  • str_split
  • str_word_count
  • substr
  • substr_compare
  • substr_count
  • substr_replace
  • trim
  • ucfirst
  • ucwords
  • vfprintf
  • vprintf
  • vsprintf
  • wordwrap
  • PHP MySQL HTML CSS JavaScript MSSQL AJAX .NET JSP Linux Mac ASP 服务器 SQL jQuery C# C++ java Android IOS oracle MongoDB SQLite wamp 交通频道