PHP Program to Split & Join a String
Last Updated :
20 Mar, 2024
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:
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
<?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;
?>
OutputAfter 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
<?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;
?>
OutputAfter 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
<?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;
?>
OutputAfter Splitting
Array
(
[0] => Hello
[1] => and
[2] => Welcome
[3] => to
[4] => GeeksforGeeks
)
After Joining
Hello_and_Welcome_to_GeeksforGeeks
Similar Reads
How to replace String in PHP ? Replacing a string in PHP involves substituting parts of a string with another string. Common methods include str_replace() for simple replacements, preg_replace() for pattern-based replacements, substr_replace() for positional replacements, and str_ireplace() for case-insensitive replacements. Each
2 min read
Shell Script to Split a String Shell Scripting or Shell Programming is just like any other programming language. A shell is a special program that provides an interface between the user and the operating system. In Linux/Unix the default shell used is bash and in windows, it is cmd(command prompt). We use the terminal to run a sh
3 min read
PHP str_pad to print string patterns str_pad: Pad a string to a certain length with another string. Syntax:- str_pad (input, pad_length, pad_string_value, pad_type) It returns the padded string. Parameters Description input:-The input string. pad_length:-If the value of pad_length is negative, less than, or equal to the length of the i
1 min read
PHP to check substring in a string In this article, we will see how to check the substring in a string. We are given two strings & we have to check whether the second string is a substring of the first string or not using PHP built-in strpos() function. This function is case-sensitive, which means that it treats upper-case and lo
2 min read
How to remove Punctuation from String in PHP ? Punctuation removal is often required in text processing and data cleaning tasks to prepare the text for further analysis or display. Below are the methods to remove punctuation from a string in PHP: Table of Content Using preg_replace with a Regular ExpressionUsing str_replace functionUsing preg_re
2 min read