All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

PHP Split String Into Array

Last Updated : Mar 11, 2024

PHP Split String Into Array

In this article we will show you the solution of PHP split string into array, in PHP, breaking up strings into smaller pieces and storing them in an array can frequently be helpful. Using regular expressions, you may construct patterns that match particular strands of text with ease.

You can divide a string into an array depending on any pattern you can design by using regular expressions.

Regular expressions can be used to break strings into arrays using the PHP preg_split() function.

You can split a string into an array based on a regular expression pattern that matches particular characters or sequences in the text.

Step By Step Guide On PHP Split String Into Array :-

You must create a regular expression pattern and send it as the first argument to the preg_split() method in order to split a text into an array using regular expressions in PHP.

You must supply the string you want to split as the function's second argument.

This method returns an array that consists of the parts of the string that are present in the string when it matches a regular expression pattern.

// Define the input string
$input_string = "Welcome to the Talkerscode.";
// Define the regular expression pattern to match on
$pattern = "/\W+/"; // This pattern will match any non-word character
$output_array = preg_split($pattern, $input_string, -1, PREG_SPLIT_NO_EMPTY);
// Print the input string and output array
echo "Input string: " . $input_string . "<br>";
echo "Output array: " . "<br>";
  1. As you can see, we have written PHP code to split a string into an array.
  2. The input string for the program's code was defined at the beginning as "Welcome to the Talkerscode."
  3. After that, the pattern is enclosed in forward slashes (/) to make it into a regular expression.
  4. In this particular case, the pattern is /W+/, which will match any character in the input string other than a word.
  5. After that, the input string is split into an array using the preg_split() function in accordance with the matched pattern.
  6. Four arguments are required by this function: $input_string, the input string to split into an array, $pattern, the regular expression pattern to match on, and -1, the maximum amount of elements to return (-1 implies to return all available elements).
  7. The parameter PREG_SPLIT_NO_EMPTY instructs the function to omit empty entries from the output array.
  8. Finally, the output array and input string are printed on the screen using the echo and print_r() functions.
  9. The output array variable $output_array is printed along with the string "Input string:" using the echo function, while the input string variable $input_string is printed using the print_r() function.

Conclusion :-

As a result, we have successfully learned how to split strings into arrays in PHP.

We also discovered that the preg_split() method in PHP allows you to quickly divide a string into an array based on a given pattern.

I hope this article on PHP split string into array helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪