PHP Split String Overview | Split String Into Array by Regular Expression
Summary: In this article, we demonstrate and explore PHP Split String Overview and also learn different way to split string into array by regular expression. Let’s understand in detail this Function with an example.
PHP Split String Definition and Usage
The PHP split string is one of all the fundamental operations for any programing language. There are various ways in PHP to deal with split relation operations. once we come back to understand the word split, we will experience the breaking of a single string into multiple strings. There are different ways we can split a string as per our needs.
Alternatives to this function include:
preg_split()
explode()
str_split()
In alternative words, if you wish to split a string to get the string character by character (1 character each), by special character, by space, etc. We can also go with the size combination while handling or dealing with the string split.
Syntax:
- Separator: A separator is a delimiter, which is the required parameter.
- String: The string that required to be broken into multiple. Again, this is a required parameter.
The limit is an optional parameter.
1 |
str_split |
1 |
str_split(String, Length) |
- String: This is a required parameter.
- Length: This is an optional parameter.
1 |
preg_split(RegularExpressionPattern, String, Limit, Flags) |
RegularExpressionPattern- it is a required parameter.
- String: it’s Required parameter.
- Limit: Optional one.
The Function of PHP Split String
We should have a string initial to perform the operation on that string. After the string, we can come to the needed delimiter upon that the string split operation required to perform.
1. The User of preg_split()
This function can be used to break a string into multiple ones based on the regular expression. this could be helpful after we have a string, let’s say date string separated by -. we are able to break that string with – key.
1 2 3 4 |
<?php $mystring = "2019-03-11 11:30:25"; // a string $outputmyArr= preg_split("/[-\s:]/", $mystring); ?> |
The following above of the code segment will break the string either by – or by the white space.
2. User of explode() Function
This function can be used to break a string into an array of strings.
Let’s understand the same with a simple example:
1 2 3 4 5 |
<?php $mystring = 'Welcome to the United States.'; // a string $myarrayString= explode(" ", $mystring ); // split string with space (white space) as a delimiter. Print_r($myarrayString); // printing the output array… ?> |
3. The user of str_split
This can also do the Identical job, as converting a string to an array of smaller strings.
1 2 3 4 |
<?php $mystring = "Welcome to United States"; // a string $outputmyArr = print_r(str_split("Welcome to United States")); ?> |
PHP Split String Example
Example 1: Split a string where we find the white space.
1 2 3 4 5 6 7 8 9 |
<?php $mystring = 'Welcome to United States.'; // a string echo "Actual String:\n"; echo $mystring; echo "\n\n"; echo "Array of string after string split:\n"; $arrayString= explode(" ", $mystring ); // split string with space (white space) as a delimiter. Print_r($arrayString); // printing the output array… ?> |
Array of string after string split:
Array (
[0] => Welcome
[1] => to
[2] => United
[3] => States.
)
Example 2: string can be split into multiple strings using a regular expression
1 2 3 4 5 6 7 8 9 |
<?php $mystring = "2019-04-17 11:25:25"; // a string echo "Actual String:\n"; echo $mystring; echo "\n\n"; echo "Array of string after string preg_split:\n"; $components = preg_split("/[-\s:]/", $mystring); print_r($components); ?> |
Array of string after string preg_split:
Array (
[0] => 2019
[1] => 04
[2] => 17
[3] => 11
[4] => 25
[5] => 25 )
Example 3: string split using the str_split() function.
1 2 3 4 5 6 7 8 9 |
<?php $mystring = "Welcome to United States"; // a string echo "Actual String:\n"; echo $mystring; echo "\n\n>"; echo "Array of string after string str_split:\n"; $arrays = str_split($mystring); print_r($arrays); ?> |
Array of string after string str_split:
Array (
[0] => W
[1] => e
[2] => l
[3] => c
[4] => o
[5] => m
[6] => e
[7] =>
[8] => t
[9] => o
[10] =>
[11] => U
[12] => n
[13] => i
[14] => t
[15] => e
[16] => d
[17] =>
[18] => S
[19] => t
[20] => a
[21] => t
[22] => e
[23] => s )
Example 4: String split using str_split() function with a specified length.
1 2 3 4 5 6 7 8 9 |
<?php $mystring = "Welcome to United States"; // a string echo "Actual String:\n"; echo $mystring; echo "\n\n"; echo "Array of string after string str_split:\n"; $arrays = str_split($mystring,4); print_r($arrays); ?> |
Array of string after string str_split:
Array (
[0] => Welc
[1] => ome
[2] => to U
[3] => nite
[4] => d St
[5] => ates )
Explanation: we will say that some string of length two or three however we’ve mentioned the length as a four. this can be simply because the string element with 2 carries 2 spaces and the string with 3 characters carry one space with it.
Example 5: To get the first string from of that string.
1 2 3 4 5 6 7 8 9 10 11 |
<?php $mystring = "What is your name?"; // a string echo "Actual String:\n"; echo $mystring; echo "\n\n"; echo "Array of string after string str_split:\n"; $arrays = explode(" ",$mystring); print_r($arrays); echo "\n\nNow getting the first element from this array:\n"; echo $arrays[0]; ?> |
Array of string after string str_split:
Array (
[0] => What
[1] => is
[2] => your
[3] => name? )
Now getting the first element from this array: What
Conclusion:
In this article, you have learned to PHP split string function and there are various ways to handle the string split. Built-in PHP functions can be used as per the needed to process the split string. I hope you will enjoy it!. if have any query related program then contact me at info@tutorialscan.com I will try to resolve the problem. Stay healthy, stay safe! Happy coding! enjoy it!