Home  >  Article  >  Backend Development  >  What are the differences between several applications of the PHP explode() function and the implode() function, explodeimplode_PHP tutorial

What are the differences between several applications of the PHP explode() function and the implode() function, explodeimplode_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:05:231263browse

What are the differences between several applications of the PHP explode() function and the implode() function, explodeimplode

explode() function introduction

The explode() function can split a string into an array.

Syntax: explode(separator,string,limit).

Parameters Description
separator Required. Specifies where to split the string.
string Required. The string to split.
limit

Optional. Specifies the number of array elements to be returned.

Possible values:

  • greater than 0 - returns an array containing at most limit
  • elements
  • less than 0 - returns an array containing all but the last -limit
  • elements
  • 0 - Returns an array containing one element

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

The separator parameter cannot be an empty string. If separator is the empty string (""), explode() returns FALSE. If separator contains a value that is not found in string, explode() returns an array containing a single element from string.

If the limit parameter is set, the returned array contains at most limit elements, and the last element will contain the remainder of the string.

If the limit parameter is negative, all but the last -limit elements are returned. This feature is new in PHP 5.1.0.

Program List: explode() example

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

Program execution result:

Apple
Banana
gonn
*

Program List: Example of explode() using limit parameter

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

Program execution result:

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

Program List: Convert string into key value array

<&#63;php
// converts pure string into a trimmed keyed array
function 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));
&#63;>

Program execution result:

Array
(
[a] =>
[b] =>
[] => $a
[c] => %
[] => true
[d] => ab c
)

PS: The difference between PHP function implode() and explode() function

The above content introduces you to the specific usage of the explode() function. When we encounter the PHP function implode(), it combines array elements into a string.

implode(separator,array)

separator optional. Specifies what is placed between array elements. Default is "" (empty string).

array required. Arrays to be combined into strings.

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

Example of PHP function implode()

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

Output:

Hello World! Beautiful Day!

The above code example is a demonstration of the specific implementation function of the PHP function implode().

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1068833.htmlTechArticleWhat are the differences between several applications of the PHP explode() function and the implode() function? Introduction to the explodeimplode explode() function The explode() function can split a string into an array. Syntax: explode(sep...
Statement:
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