In PHP, implode()
and explode()
are two commonly used functions for working with strings. These functions allow you to split and join strings easily. In this article, we will explore these functions in detail and show you how to use them effectively in your PHP code.
Implode and explode function in PHP; Through this tutorial, you will learn everything about implode() and explode() function in PHP.
We can use both functions with PHP arrays and strings. And here also take examples like an array to string conversion in PHP, the string to array conversion in PHP.
Implode and Explode in PHP
Let’s see the implode and explode function in PHP; is as follows:
- PHP implode() Function
- PHP Explode Function
PHP implode() Function
Implode function is a built-in function of php, which is used to convert array to string by provided delimiter.
Here is the basic syntax for the implode()
function:
implode(glue
,pieces
)
Parameters of implode function
$glue
– The string to use as a delimiter when joining the substrings.$pieces
– The array of substrings to join.
Here is an example of how to use implode()
:
$fruits = array("Apple", "Banana", "Orange", "Mango"); $string = implode(", ", $fruits); echo $string;
In this example, we use the implode()
function to join the $fruits
array into a single string. We specify the delimiter as ", "
, which means that each substring in the array will be separated by a comma followed by a space. The resulting string will be “Apple, Banana, Orange, Mango”.
PHP Explode Function
Explode function is a built-in function of php, which is used to convert/break string to an array by a provided delimiter.
Here is the basic syntax for the explode()
function:
explode(delimiter
,string,limit)
Parameters of explode function
$delimiter
– The character or characters that are used to split the string.$string
– The string to split.$limit
– Optional parameter that specifies the maximum number of substrings to return. If this parameter is omitted or set to0
, all substrings are returned.
Here is an example of how to use explode()
:
Example 1:- string to array conversion in php
$string = "Apple, Banana, Orange, Mango"; $fruits = explode(", ", $string); print_r($fruits);
In this example, we use the explode()
function to split the $string
variable into an array of substrings. We specify the delimiter as ", "
, which means that the string will be split wherever there is a comma followed by a space. The resulting array will contain four elements: “Apple”, “Banana”, “Orange”, and “Mango”.
Conclusion
The explode()
and implode()
functions are simple yet powerful tools for working with strings in PHP. The explode()
function allows you to split a string into an array of substrings, while the implode()
function allows you to join an array of substrings into a single string. By using these functions effectively, you can easily manipulate and work with strings in your PHP code.
Recommended PHP Tutorials
- Compare Arrays PHP | PHP array_diff() Function
- Get, Write, Read, Load, JSON File from Url PHP
- Functions: Remove First Character From String PHP
- Remove Specific/Special Characters From String In PHP
- How to Replace First and Last Character From String PHP
- Reverse String in PHP
- Array Push, POP PHP | PHP Array Tutorial
- PHP Search Multidimensional Array By key, value and return key
- json_encode()- Convert Array To JSON | Object To JSON PHP
- PHP remove duplicates from multidimensional array
- PHP Remove Duplicate Elements or Values from Array PHP
- Get Highest Value in Multidimensional Array PHP
- PHP Get Min or Minimum Value in Array
- String PHP to Uppercase, Lowercase & First Letter Uppercase
- PHP: isset() vs empty() vs is_null()
- Chunk_split PHP Function Example
- PHP Function: Date and Time With Examples
- Reverse Number in PHP | PHP Tutorial