PHP Program to Split & Join a String Last Updated : 20 Mar, 2024 Comments Improve Suggest changes Like Article Like Report 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() FunctionUsing str_split() and Custom Join FunctionUsing Regular Expressions with preg_split() and implode() FunctionUsing explode() and implode() FunctionThe 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_GeeksUsing str_split() and Custom Join FunctionThe 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_dUsing Regular Expressions with preg_split() and implode() FunctionFor 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 Comment More infoAdvertise with us Next Article PHP Program to Split & Join a String A ashokjaiswal Follow Improve Article Tags : PHP 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 Bash Scripting - Split String In this article, we will discuss how to split strings in a bash script. Dividing a single string into multiple strings is called string splitting. Many programming languages have a built-in function to perform string splitting but there is no built-in function in bash to do so. There are various met 4 min read PHP Reverse a String Reversing a string in PHP refers to rearranging a given string's characters in reverse order, starting from the last character to the first. This task is often used in text manipulation or algorithm challenges, highlighting PHP's string-handling capabilities.Examples: Input : GeeksforGeeksOutput : s 3 min read Insert string at specified position in PHP Given a sentence, a string and the position, the task is to insert the given string at the specified position. We will start counting the position from zero. See the examples below. Input : sentence = 'I am happy today.' string = 'very' position = 4 Output :I am very happy today.Input : sentence = ' 3 min read How to limit string length in PHP ? A string is a sequence of characters in PHP. The length of the string can be limited in PHP using various in-built functions, wherein the string character count can be restricted. Table of ContentUsing for loopUsing mb_strimwidth() function Using substr() method Using Regular Expressions with preg_m 6 min read PHP | preg_split() Function The preg_split() function is an inbuilt function in PHP which is used to convert the given string into an array. The function splits the string into smaller strings or sub-strings of length which is specified by the user. If the limit is specified then small string or sub-strings up to limit return 3 min read Like