Updated April 15, 2023
Introduction to PHP Split String
The split string is one of the basic operations for any programming language. There are various built-in functions in PHP to deal with split relation operations. When we come to know the word split, we will experience the breaking of a single string into the multiple strings. There are various ways we can split a string as per our business requirements. In other words, if you want to split a string to get the string character by character (1 character each), by space, by special character etc. We can also go with the size combination while dealing with the string split.
Syntax:
explode(Separator, String, Limit)
- Separator: Separator is a delimiter, that is the required parameter.
- String: The string that needs to be broken into multiple. Again, this is a required parameter.
The limit is an optional parameter.
str_split
str_split(String, Length)
- String: This is a required parameter.
- Length: This is an optional parameter.
preg_split(RegularExpressionPattern, String, Limit, Flags)
RegularExpressionPattern- required parameter.
- String: Required parameter.
- Limit: Optional one.
Working of PHP Split String
Before moving ahead with the string split, we must have a string first to perform the operation on that string. After string, we can come to the required delimiter upon which the string split operation needs to perform.
1. User of explode() Function
This function can be used to break a string into an array of string.
Let’s understand the same with a basic example:
$string = 'Welcome to the India.'; // a string
$arrayString= explode(" ", $string ); // split string with space (white space) as a delimiter.
Print_r($arrayString); // printing the output array…
2. The User of preg_split()
This function can be used to break a string into multiple ones based on regular expression. This can be useful when we have a string, let’s say date string separated by -. We can break that string with – key.
$string = "2020-06-21 10:10:20"; // a string
$outputArr= preg_split("/[-\s:]/", $string);
The above of the code will break the string either by – or by the white space.
3. The user of str_split
This can also do the same job, converting a string to an array of smaller strings.
$string = "Welcome to the India"; // a string
$outputArr = str_split("Welcome to the India");
Examples to Implement PHP Split String
Below are the Example of PHP Split String:
Example #1
Split a string where we find the white space.
Code:
<?php
$string = 'Welcome to the India.'; // a string
echo "Actual String:\n";
echo $string;
echo "\n\n";
echo "Array of string after string split:\n";
$arrayString= explode(" ", $string ); // split string with space (white space) as a delimiter.
Print_r($arrayString); // printing the output array…
?>
Output:
Example #2
In this example, we will see how a string can be split into multiple strings using a regular expression.
Code:
<?php
$string = "2020-06-21 10:10:20"; // a string
echo "Actual String:\n";
echo $string;
echo "\n\n";
echo "Array of string after string preg_split:\n";
$components = preg_split("/[-\s:]/", $string);
print_r($components);
?>
Output:
Example #3
Let’s see another very simple example of string split using the str_split() function.
Code:
<?php
$string = "Welcome to the India"; // a string
echo "Actual String:\n";
echo $string;
echo "\n\n>";
echo "Array of string after string str_split:\n";
$arrays = str_split($string);
print_r($arrays);
?>
Output:
Example #4
String split using str_split() function with a specified length.
Code:
<?php
$string = "Welcome to the India"; // a string
echo "Actual String:\n";
echo $string;
echo "\n\n";
echo "Array of string after string str_split:\n";
$arrays = str_split($string,4);
print_r($arrays);
?>
Output:
Explanation: Looking at the output of the above program, we can say that some string of length 2 or 3 but we have mentioned the length as a 4. This is just because the string element with 2 carries 2 spaces and the string with 3 characters carry one space with it.
Example #5
Now, using the string split method lets try to get the first string from of that string.
Code:
<?php
$string = "What do you want to learn?"; // a string
echo "Actual String:\n";
echo $string;
echo "\n\n";
echo "Array of string after string str_split:\n";
$arrays = explode(" ",$string);
print_r($arrays);
echo "\n\nNow getting the first element from this array:\n";
echo $arrays[0];
?>
Output:
Explanation: In the same way, we can access the other elements of the string. For example, $arrays[1] to access the 2nd element.
Conclusion
We can simply say, in PHP there are various ways to handle the string split. Built-in PHP functions can be used as per the business requirements to process the split string. But code or developer should be aware of using these and the pros and cons of it. Most of the functions of the string split changed the string into an array then we can get the specified string from that array.
Recommended Article
This is a guide to the PHP Split String. Here we discuss the Introduction, syntax, and working of Split String in PHP along with different examples and code implementation. You can also go through our other suggested articles to learn more –