PASSWORD RESET

Your destination for complete Tech news

PHP

Implode and explode functions in PHP with examples.

382 0
< 1 min read

In PHP, the implode function is used to join elements of an array into a single string. It takes two arguments: a string that separates the array elements in the resulting string, and an array to join.

For example:

$array = ['apple', 'banana', 'orange'];
$string = implode(', ', $array);

// $string is now 'apple, banana, orange'

The explode function is the opposite of implode. It takes a string and a delimiter as arguments and returns an array of strings created by splitting the input string at each occurrence of the delimiter.

For example:

$string = 'apple, banana, orange';
$array = explode(', ', $string);

// $array is now ['apple', 'banana', 'orange']

You can also specify a limit argument to the explode function to control the maximum number of array elements that should be returned. For example:

$string = 'apple, banana, orange, lemon, lime';
$array = explode(', ', $string, 3);

// $array is now ['apple', 'banana', 'orange, lemon, lime']

The explode function is often used to parse simple delimiter-separated strings, such as CSV (comma-separated values) or TSV (tab-separated values) files.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.