• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

Using PHPs implode() and explode() Built-in Functions

manhgame1

Raid Leader
M Rep
0
0
0
Rep
0
M Vouches
0
0
0
Vouches
0
Posts
39
Likes
196
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 2 1000 XP
In this article, you will learn how to use the PHPs implode() and explode() built-in functions. The tutorial aims to provide the IT/CS students and new programmers a reference for widening their knowledge and enhancing their PHP Programming capabilities. Here, snippets that demonstrate the usage of the said PHP functions are provided.

PHP comes with built-in methods and functions that are helpful and useful for developers to achieve their desired features for their projects or web applications. The implode() and explode() functions are one of these and are available from PHP version 4 up to the latest which is PHP8. These functions are used for manipulating arrays and strings.

What is implode() in PHP?

The implode() function in PHP is a built-in function that allows you to combine the array of data into one string. Developers can simply join the array items and set a string between each item.

How to use PHP implode() function?

<?php

implode

(

str $seperator

,

array

$array

)

?>

Parameters

  • (str)$seperator
    • The string that is placed between the array items when combined.
  • (arr)$array
    • The array of strings to combine or implode.

Example

Here is an example snippet that demonstrates the usage of implode() function of PHP.

  1. <?php
  2. //Sample Arrays
  3. $array1

    =

    array

    (

    "Hello"

    ,

    "Wold!!!"

    )

    ;
  4. $array2

    =

    array

    (

    "I"

    ,

    "Love"

    ,

    "SourceCodester!"

    )

    ;
  5. $array3

    =

    array

    (

    "PHP"

    ,

    "JavaScript"

    ,

    "Python"

    )

    ;

  6. //Combining Arrays
  7. $arrtostring1

    =

    implode

    (

    " "

    ,

    $array1

    )

    ;
  8. $arrtostring2

    =

    implode

    (

    " "

    ,

    $array2

    )

    ;
  9. $arrtostring3

    =

    implode

    (

    ", "

    ,

    $array3

    )

    ;

  10. echo

    $array1

    .

    "<br>"

    ;
  11. echo

    $array2

    .

    "<br>"

    ;
  12. echo

    $array3

    ;
  13. ?>

Result

What is explode() in PHP?

The explode() function in PHP is a built-in function that allows you to split the string into pieces. The developers can explode the string using the given separator. The explode function result can be limited depending on the developer's given count of items to return.

How to use PHP explode() function?

  1. <?php

    explode

    (

    str $seperator

    ,

    str $string

    ,

    int $limit

    delault:

    PHP_INT_MAX)

    ?>

Parameters

  • (str)$seperator
    • The string checks and serves as the boundaries of the items for splitting.
  • (str)$string
    • The string to split using the method/function.
  • (int)$limitDefault:

    PHP_INT_MAX


    • The limit count of the result's return array items and if the entered value is positive.
    • If negative, the function will return all the items except the last (n) item/s.

Example

Here is an example snippet that demonstrates the usage of explode() function of PHP.

  1. <?php
  2. $string1

    =

    "Hello Wold!!!"

    ;
  3. $string2

    =

    "I Love SourceCodester!"

    ;
  4. $string3

    =

    "PHP, JavaScript, Python"

    ;
  5. $splitString1

    =

    explode

    (

    " "

    ,

    $string1

    )

    ;
  6. $splitString2

    =

    explode

    (

    " "

    ,

    $string2

    )

    ;
  7. $splitString3

    =

    explode

    (

    ", "

    ,

    $string3

    ,

    -

    1

    )

    ;

  8. print_r

    (

    $splitString1

    )

    ;
  9. echo

    "<br>"

    ;
  10. print_r

    (

    $splitString2

    )

    ;
  11. echo

    "<br>"

    ;
  12. print_r

    (

    $splitString3

    )

    ;
  13. ?>

Result

There you go! I hope this Usage of PHPs implode() and explode() built-in function tutorial helps you and enhances your programming knowledge and capabilities.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding :)


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,500

350,639

350,649

Top