PHP Program to Split & Join a String
Given a String, the task is to split and join strings in PHP. It is a common operations that allow you to manipulate and manage text data effectively.
Below are the approaches to split and join a string in PHP:
Table of Content
Using explode() and implode() Function
The explode() function is used to split a string into an array based on a delimiter. It is used when you want to break the given string into individual words. The implode() function is used to join array elements into a single string.
- explode(" ", $string) function splits the string into an array using the space character as the delimiter.
- implode(" ", $string) function joins the elements of the array back into a single string using the space character as the delimiter.
Example: This example shows the use of the above-explained approach.
<?php
// Original string
$string = "Hello and Welcome to Geeks for Geeks";
// Splitting the string into individual words
$splitString = explode(" ", $string);
echo "After Splitting\n";
print_r($splitString);
// Joining the array elements
$joinedString = implode("_", $splitString);
echo "\nAfter Joining\n";
echo $joinedString;
?>
Output
After Splitting Array ( [0] => Hello [1] => and [2] => Welcome [3] => to [4] => Geeks [5] => for [6] => Geeks ) After Joining Hello_and_Welcome_to_Geeks_for_Geeks
Using str_split() and Custom Join Function
The str_split() function splits a string into an array of characters. For joining, we can create a custom function that concatenates array elements. It will concatenate the array elements into a single string, adding the separator between elements, and returns the resulting string.
Example: This example shows the use of the above-explained approach.
<?php
// Original string
$string = "HelloWorld";
// Split the string into characters
$splitString = str_split($string);
echo "After Splitting\n";
print_r($splitString);
// Custom function to join array elements
function customJoin($array, $separator = "_") {
$joinedString = "";
foreach ($array as $element) {
$joinedString .= $element . $separator;
}
return rtrim($joinedString, $separator);
}
// Joining the characters
$joinedString = customJoin($splitString);
echo "\nAfter Joining\n";
echo $joinedString;
?>
Output
After Splitting Array ( [0] => H [1] => e [2] => l [3] => l [4] => o [5] => W [6] => o [7] => r [8] => l [9] => d ) After Joining H_e_l_l_o_W_o_r_l_d
Using Regular Expressions with preg_split() and implode() Function
For more complex splitting patterns, you can use regular expressions with the preg_split() function. The implode() function will be used to join array elements into a single string.
- preg_split("/[\s,]+/", $string) splits the string $string into an array using a regular expression that matches one or more spaces or commas.
Example: This example shows the use of the above-explained approach.
<?php
// Original string
$string = "Hello and Welcome to GeeksforGeeks";
// Splitting the string using regular expression
$splitString = preg_split("/[\s,]+/", $string);
echo "After Splitting\n";
print_r($splitString);
// Joining the array elements
$joinedString = implode("_", $splitString);
echo "\nAfter Joining\n";
echo $joinedString;
?>
Output
After Splitting Array ( [0] => Hello [1] => and [2] => Welcome [3] => to [4] => GeeksforGeeks ) After Joining Hello_and_Welcome_to_GeeksforGeeks