Tutorial - Explode and Implode in PHP
What Will I Learn?
- How to learn Explode and Implode in PHP
Requirements
- XAMPP
- Editor Text
Difficulty
- Basic
Preliminary
Explode and Implode are functions provided by PHP to separate or merge strings. Strings that are separated or stratified (merge) will be the form of arrays that have their respective numbering. For more detail the explanation is below :
- Explode is a function that aims as a string separator. So a string will be solved according to the self-defined dash. A parsed string will be an array. Explode writing is
explode()
. - Implode is a function that aims to recombine (stratify/merge) previously solved strings by the explode function.
Implode writing isimplode()
.
To know how to write these two functions in PHP, let's get started.
Practice Explode and Implode
- Explode Function
The explode()
function in PHP is used to convert strings into arrays. In separating a string into an array, the explode()
function takes several arguments.
Here is a sample program code using the explode function below :
<?php
echo "<h2>simpleawesome here</h2>";
echo "<br/>";
$sentences = "learn explode implode in php";
// separating the string into an array
$data = explode(" " , $sentences);
// string into an array
print_r($data);
//print_r allows to display the contents of the array
echo "<br/>";
echo "<br/>";
echo " Data 1 = " . $data[0];
echo "<br/>";
echo " Data 2 = " . $data[1];
echo "<br/>";
echo " Data 3 = " . $data[2];
echo "<br/>";
echo " Data 4 = " . $data[3];
echo "<br/>";
echo " Data 5 = " . $data[4];
?>
Then, type the program code in the text editor.
Here are the results :
Look at the program code we have created earlier above, here we have a string in the sentences
variable :
$sentences = "learn explode implode in php";
and then we separate the above string into an array using the explode()
function :
// separating the string into an array
$data = explode(" " , $sentences);
In the explode function there is a separator between words. Here we fill the separator with space. So string results will be separated by a space. Until here, the data variable already contains the data string that has become an array.
// string into an array
print_r($data);
//print_r allows to display the contents of the array
And then to display the string one by one (data separated) using the following command :
echo " Data 1 = " . $data[0];
echo "<br/>";
echo " Data 2 = " . $data[1];
echo "<br/>";
echo " Data 3 = " . $data[2];
echo "<br/>";
echo " Data 4 = " . $data[3];
echo "<br/>";
echo " Data 5 = " . $data[4];
Results are displayed as many as 5 data, because the string in the input on the $sentence
variable amounted to 5 words.
- Implode Function
The implode()
function is PHP's default function for combining arrays into strings. This function is also the opposite of the explode()
function. If explode is a string separator, then implode is a merge of string that has been solved by explode. Here is a sample program code using the implode function below :
<?php
echo "<h2>simpleawesome here</h2>";
echo "<br/>";
$sentences = "learn explode implode in php";
// separating the string into an array
$data = explode(" " , $sentences);
// string into an array
print_r($data);
//print_r allows to display the contents of the array
echo "<br/>";
echo "<br/>";
echo " Data 1 = " . $data[0];
echo "<br/>";
echo " Data 2 = " . $data[1];
echo "<br/>";
echo " Data 3 = " . $data[2];
echo "<br/>";
echo " Data 4 = " . $data[3];
echo "<br/>";
echo " Data 5 = " . $data[4]
echo "<br/>";
echo "<br/>";
echo "<h3>Combine (merge) string with implode</h3>";
echo "<br/>";
// combine (merge) return string with implode() function
echo implode(" ",$data);
?>
Then, type the program code in the text editor.
The above code is a program code that we have previously created for the explode function. Only there is the addition of new commands. So the goal here is to combine strings that have been separated by using implode using the command :
// combine (merge) return string with implode() function
echo implode(" ",$data);
The string will return to a sentence.
Curriculum
- Tutorial - How to run PHP Programming Language in Command Prompt (CMD)
- Tutorial - Method Chaining in PHP
- Tutorial - Constructor and Destructor in PHP
Posted on Utopian.io - Rewarding Open Source Contributors
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Thank you @deathwing
Hey @simpleawesome I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x