Tutorial - Explode and Implode in PHP

in #utopian-io8 years ago (edited)

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 is implode().

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.
explode.PNG

Here are the results :

exploderesult.PNG

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.
implode.PNG

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);

Here are the results :
imploderesult.PNG

The string will return to a sentence.

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @simpleawesome I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

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

Coin Marketplace

STEEM 0.13
TRX 0.34
JST 0.033
BTC 116555.50
ETH 4489.25
SBD 0.86