Home Backend Development PHP Tutorial Instructions for using php explode() function and implode() function

Instructions for using php explode() function and implode() function

Jul 07, 2017 am 10:03 AM
explode implode Instructions for use

The following describes the usage of explode and implode in php, ballet shoes, I hope it will be helpful to my friends!

Speaking of php,Function is very important. It is also something that PHP enthusiasts and friends who like PHP advanced must master. Friends who are learning PHP also know that arrays must also be mastered. It can be said that, Advanced php, that is, learning arrays in php. In arrays, we usually need to split string or something like that. We need to use explode and implode function. The following explains the explode in php. And the usage of implode, ballet shoes, I hope it will be helpful to my friends!
Usage of explode in PHP
(PHP 3, PHP 4, PHP 5) explode--use a string to split another character String description
array explode ( string separator, string string [, int limit] )
This function returns an array composed of strings. Each element is a substring of string. They are separated by string separators as boundary surfaces.

If the limit parameter is set, the returned array contains at least limit elements, and the last element will contain the remaining part of string.
If separator is an empty string (""), explode() will return FALSE.
If the value contained in separator cannot be found in string, explode() will return an array containing the two elements of string.
If the limit parameter is a negative number, all elements except the last limit elements are returned. This feature is new in PHP 5.1.0.

Due to historical reasons, although implode() can accept two types of parameter orders, explode() cannot. You must ensure that the separator parameter comes before the string parameter.

Note: The parameter limit was added in PHP 4.0.1.

Example 1.
explode() example

The code is as follows:

<?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; // * 
?>
Copy after login

Example 2.
limit parameter example

The code is as follows:

<?php 
$str = &#39;one|two|three|four&#39;; // 正数的 
limit print_r(explode(&#39;|&#39;, $str, 2)); // 负数的 
limit print_r(explode(&#39;|&#39;, $str, -1)); 
?>
Copy after login

The above example will output: Array ([0] => one [1] => two|three|four ) Array ( [0] => one [1 ] => two [2] => three)
Note: When the function can be used safely for two objects.
PHP implode() function

Example
Output:
Hello World! Beautiful Day!

Definition and usage The implode() function combines array elements into a string.
Syntax implode(separator,array)
Parameter description separator is optional. Specifies what is placed between array elements. The default is "" (empty string). Array must be 400 phone number. Arrays to be combined into strings. Note that the separator parameter is optional. However, for the sake of backward compatibility, it is recommended that you use two parameters.
Tips and text text: implode() can accept two types of parameter orders. But due to historical reasons, explode() is endless. You must ensure that the separator parameter comes before the string parameter.

PHP’s explode and implode

The code is as follows:

<?php 
$zongzi = "1|2|3|4|5|6"; 
$zongzi = explode("|",$zongzi); 
var_dump($zongzi); 
$zongzi = implode("|",$zongzi); 
echo $zongzi; 
?>
Copy after login

The above is the detailed content of Instructions for using php explode() function and implode() function. For more information, please follow other related articles on the PHP Chinese website!

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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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

In PHP, we often need to combine elements in an array into a string. At this time, the implode() function comes in handy. The implode() function converts the elements in the array into strings and concatenates them to produce a complete string. This article will introduce how to use the implode() function in PHP to convert an array into a string. 1. Basic usage of implode() function The implode() function has two parameters. The first parameter

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

The explode function in PHP is a function used to split a string into an array. It is very common and flexible. In the process of using the explode function, we often encounter some errors and problems. This article will introduce the basic usage of the explode function and provide some methods to solve the error reports. 1. Basic usage of the explode function In PHP, the basic syntax of the explode function is as follows: explode(string$separator,string$stri

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

Title: Common errors and solutions when using the explode function in PHP In PHP, the explode function is a common function used to split a string into an array. However, some common errors can occur due to improper use or incorrect data format. This article will analyze the problems you may encounter when using the explode function, and provide solutions and specific code examples. Mistake 1: The delimiter parameter is not passed in. When using the explode function, one of the most common mistakes is that the delimiter parameter is not passed in.

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

In PHP programming, Warning messages often appear. One of the more common prompts is "PHPWarning:implode():Invalidargumentspassed". This article will introduce the ideas and methods to solve this Warning. First, we need to understand how to use the implode() function. The function of this function is to concatenate an array into a string, using the following syntax: string

How to use OnePlus budsPro_OnePlus budsPro instructions How to use OnePlus budsPro_OnePlus budsPro instructions Mar 23, 2024 am 10:11 AM

1. Switch between noise reduction mode and transparency mode. Press and hold the handle of the earphones for about 1 second to switch between noise reduction mode and transparency mode. 2. In music mode, press the earphone handle once to pause or play music. Press the earphone handle twice to play the next song. Press the earphone handle three times to play the previous song or wake up the voice. 3. In call mode, during a call, press the earphone handle once to answer or hang up the call. 4. How to reset Open the earphone box. When the charging box indicator light flashes red 5 times, release the button and the earphones are reset. 3. How to connect the phone 1. Open the charging box 2. Press and hold the setting button for 2 seconds 3. When a pop-up window appears on the phone screen, click to confirm the connection. 4. How to check the battery status 1. When the earphones are connected to the mobile phone, you can check the battery level of the earphones and charging box in the pop-up window on the mobile phone screen. 2,

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

In PHP programming, the implode function is a very commonly used function that can concatenate elements in an array into a string. Using this function can save developers from writing a lot of code to connect strings, making it more efficient. The basic syntax of implode is: stringimplode(string$glue,array$pieces). This function receives two parameters: $glue represents the separator to connect the array elements, and $pieces represents

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

In PHP programming, the implode() function is a very commonly used function. This function is mainly used to concatenate elements in an array to form a string. The use of this function is very simple and flexible. It allows us to save time and code in the process of splicing strings. In this article, we will introduce PHP's implode() function in detail so that we can use it better. Basic syntax The basic syntax for using the implode() function in PHP is as follows: implode(separa

Instructions for accessing and using the payment function of UniApp Instructions for accessing and using the payment function of UniApp Jul 04, 2023 am 10:27 AM

Instructions for accessing and using the payment function of UniApp. With the popularity of mobile payment, many applications need to integrate payment functions to facilitate users to make online payments. As a cross-platform development framework based on Vue.js, UniApp has the characteristics of one-time development and multi-platform use, and can easily implement the payment function. This article will introduce how to access the payment function in UniApp and give code examples. 1. To access the payment function, add payment permissions in the manifest.json file on the App side:

See all articles