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.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax:
explode(Separator, String, Limit)
The limit is an optional parameter.
str_split
str_split(String, Length)
preg_split(RegularExpressionPattern, String, Limit, Flags)
RegularExpressionPattern- required parameter.
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.
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…
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.
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");
Below are the Example of PHP Split String:
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:
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:
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:
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.
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.
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.
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 –
The above is the detailed content of PHP Split String. For more information, please follow other related articles on the PHP Chinese website!