Home php教程 php手册 PHP的explode和implode的使用说明

PHP的explode和implode的使用说明

Jun 06, 2016 pm 08:38 PM
explode implode

下里说道php中的explode 和implode的用法,芭蕾舞鞋,盼望对于友朋们有所辅助!

说到php,函数便长短常主要的,也是php爱好者,和喜好php进阶的朋友们必须控制的东东,学习php的友朋们也晓得,数组也是必需把握的,能够那样道,进阶php,也便是学习php中的数组而在数组中通常要用到分割字符串啊什么的,便要用到explode ,和implode函数,下里说道php中的explode 和implode的用法,芭蕾舞鞋,盼望对于友朋们有所辅助!
explode在PHP中的用法
(PHP 3, 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 参数之前才行。

注: 参数 limit 是正在 PHP 4.0.1 中参加的。

例子 1.
explode() 示例
代码如下:
// 示例 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 参数示例
代码如下:
$str = 'one|two|three|four'; // 正数的
limit print_r(explode('|', $str, 2)); // 负数的
limit print_r(explode('|', $str, -1));
?>

以上示例将输出: Array ([0] => one [1] => two|three|four ) Array ( [0] => one [1] => two [2] => three)
注: 当函数可平安用于两入造对于象。
PHP implode() 函数

例子
输出:
Hello World! Beautiful Day!

订义和用法implode() 函数把数组元素组合为一个字符串。
语法implode(separator,array)
参数描写separator可选。划定数组元素之间搁放的内容。默许是 ""(空字符串)。array必须,400电话。要联合为字符串的数组。阐明固然 separator 参数是可选的。但是为了背后兼容,推举您使用使用两个参数。
提醒和正文正文:implode() 能够接受两类参数次序。但是因为历史缘由,explode() 是没有止的。您必需保证 separator 参数正在 string 参数之前才止。

PHP的explode和implode
代码如下:
$zongzi = "1|2|3|4|5|6";
$zongzi = explode("|",$zongzi);
var_dump($zongzi);
$zongzi = implode("|",$zongzi);
echo $zongzi;
?>
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Convert array to string using implode() function in PHP Convert array to string using implode() function in PHP Jun 26, 2023 pm 11:51 PM

Convert array to string using implode() function in PHP

How to use PHP explode function and solve errors How to use PHP explode function and solve errors Mar 10, 2024 am 09:18 AM

How to use PHP explode function and solve errors

PHP Warning: implode(): Invalid arguments passed solution PHP Warning: implode(): Invalid arguments passed solution Jun 23, 2023 am 09:04 AM

PHP Warning: implode(): Invalid arguments passed solution

Split and merge strings using the explode and implode functions Split and merge strings using the explode and implode functions Jun 15, 2023 pm 08:42 PM

Split and merge strings using the explode and implode functions

Common errors and solutions when using the explode function in PHP Common errors and solutions when using the explode function in PHP Mar 11, 2024 am 08:33 AM

Common errors and solutions when using the explode function in PHP

How to use implode function to concatenate array elements into string in PHP How to use implode function to concatenate array elements into string in PHP Jun 26, 2023 pm 02:02 PM

How to use implode function to concatenate array elements into string in PHP

Introduction to the usage of PHP implode() function Introduction to the usage of PHP implode() function Jun 27, 2023 am 10:56 AM

Introduction to the usage of PHP implode() function

Split string into array using PHP function 'explode' Split string into array using PHP function 'explode' Jul 24, 2023 pm 11:09 PM

Split string into array using PHP function 'explode'

See all articles