What are the differences between several applications of the PHPexplode () function and the implode () function? the explodeimplode_PHP tutorial

Source: Internet
Author: User
Tags php explode
What is the difference between several applications of the PHPexplode () function and the implode () function? explodeimplode. What are the differences between several applications of the PHPexplode () function and the implode () function? the explodeimplodeexplode () function introduces The explode () function, which can divide strings into arrays. Syntax: explode (sep PHP explode () function and implode () function are different, explodeimplode

Explode () function introduction

The explode () function can split strings into arrays.

Syntax: explode (separator, string, limit ).

Parameters Description
Separator Required. Specifies where to split the string.
String Required. The string to be split.
Limit

Optional. Specifies the number of returned array elements.

Possible values:

  • Greater than 0-the maximum value is returned.LimitArray of elements
  • Less than 0-return contains except the last-LimitArray of all elements other than elements
  • 0-returns an array containing an element.

This function returns an array composed of strings. each element is a substring separated by a separator as a boundary point.

The separator parameter cannot be a null string. If separator is a null string (""), explode () returns FALSE. If the value of separator cannot be found in string, explode () returns an array containing a single element in string.

If the limit parameter is set, the returned array contains up to limit elements, and the last element contains the rest of the string.

If the limit parameter is negative, all elements except the last-limit element are returned. This feature is added in PHP 5.1.0.

Example of Program List: explode ()

<?php// Example $fruit = "Apple Banana Orange Lemon Mango Pear";$fruitArray = explode(" ", $fruit);echo $fruitArray[]; // Appleecho $fruitArray[]; // Banana// Example $data = "gonn:*:nowamagic:::/home/foo:/bin/sh";list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);echo $user; // gonnecho $pass; // *?>

Program running result:

Apple
Banana
Gonn
*

Program List: explode () example using the limit parameter

<?php$str = 'one|two|three|four';// positive limitprint_r(explode('|', $str, ));// negative limit (since PHP .)print_r(explode('|', $str, -));?>

Program running result:

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

Program List: converts a string into a key-value array.

<?php// converts pure string into a trimmed keyed arrayfunction stringKeyedArray($string, $delimiter = ',', $kv = '=>') { if ($a = explode($delimiter, $string)) { // create parts  foreach ($a as $s) { // each part   if ($s) {    if ($pos = strpos($s, $kv)) { // key/value delimiter     $ka[trim(substr($s, , $pos))] = trim(substr($s, $pos + strlen($kv)));    } else { // key delimiter not found     $ka[] = trim($s);    }   }  }  return $ka; }} // stringKeyedArray$string = 'a=>, b=>, $a, c=>%, true, d=>ab c';print_r(stringKeyedArray($string));?>

Program running result:

Array
(
[A] =>
[B] =>
[] => $
[C] => %
[] => True
[D] => AB c
)

PS: differences between PHP functions implode () and explode ()

The above content introduces the usage of the explode () function. When we encounter the PHP function implode (), the array elements are combined into a string.

Implode (separator, array)

Optional. Specifies the content to be placed between array elements. The default value is "" (null string ).

Array is required. Array to be combined as a string.

Although the separator parameter is optional. However, we recommend that you use two parameters for backward compatibility.

Example of PHP function implode ()

<?php $arr = array('Hello','World!','Beautiful','Day!'); echo implode(" ",$arr); ?> 

Output:

Hello World! Beautiful Day!

The above code example shows the specific implementation functions of the PHP function implode.

The http://www.bkjia.com/PHPjc/1068833.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1068833.htmlTechArticlePHP explode () function of several applications and implode () function has what difference, explodeimplode explode () function introduction explode () function can divide the string into an array. Syntax: explode (sep...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.