Split function in PHP
A language that understands scripts at runtime is known as a scripting language. Typically, scripts are used to improve an application’s performance or carry out necessary activities. The following alternatives to the split function are covered in full in this article on splitting in PHP.
Enroll for the Best PHP Training in Chennai at Softlogic Systems to become a master in web page development.
Split in PHP
In Split Function in PHP 5.3.0, the split function was rejected, and in PHP 7, it was eliminated. There are substitutes for this function, including:
- preg_split()
- explode()
- str_split()
What is preg_split()?
The PHP built-in function may be used to turn the supplied text into an array. Using this function, the text is divided into shorter substrings whose lengths are supplied by the user. If the limit is set, substrings will return through an array up to the limit.
Syntax
pattern : When looking for a pattern, it is of the string type; otherwise, it divides the pieces.
subject : It is a variable that serves as a storage location for the input string.
limit : It makes the limit clear. Sub-strings must be returned up to the limit if a limit is given. The flag is used when the limit is 0 or -1, which denotes “no limit.”
flag :
PREG SPLIT NO EMPTY flag (): Only non-empty parts will be returned by this
PREG SPLIT DELIM CAPTURE: Parenthesized expression in the delimiter pattern will also be captured and returned.
PREG SPLIT OFFSET CAPTURE: For each occurrence of a match, the associated string offset will also be returned.
You can divide the word by any quantity of commas or space characters as follows:
<pre><?php $variable = preg_split(“/[s,]+/”, “reka menon, saranya prince”); print_r($variable); ?>
</pre>
Output
Array(
[0] => reka
[1] => menon
[2] => saranya
[3] => prince
)
We used this to separate a string into its individual characters:
Output
Array(
[0] => r
[1] =>e
[2] =>k
[3] =>a
)
We divide a string into matches and their offsets in the following manner:
Output
Array
(
[0] => Array
(
[0] =>reka
[1] => 0
)
[1] => Array
(
[0] =>is
[1] =>5
)
[2] => Array
(
[0] =>now
[1] =>8
)
[3] => Array
(
[0] =>ready
[1] => 11
)
)
What is explode()?
The function turns a string into an array and is built-in.
Syntax
Separator: The string to be divided is determined by the separator character.
String: An array will be created from this input string.
Limit: This defines the number of elements in an array and is optional.
Split Function in PHP: Example
<pre><?php $var_str = ‘january,february,march,april’; print_r(explode(‘,’,$var_str,0)); //zero limit print_r(explode(‘,’,$var_str,2));// positive limit print_r(explode(‘,’,$var_str,-1));// negative limit ?>
</pre>
Output
Array
(
[0] => january,february,march,april
)
Array
(
[0] => january
[1] => february,march,april
)
Array
(
[0] => january
[1] => february
[2] =>march
)
What is str_split()?
With the use of the length argument, this method may divide a string into an array.
Syntax
str_split(string,length())
String: It denotes the string to split
Length: It denotes the length of each array element. (Default is 1)
Split Function in PHP: Example
<pre><?php print_r(str_split(“reka”,3)); ?>
</pre>
Output
Array(
[0] => rek
[1] => a
)
Conclusion
We hope this article has provided you with a thorough introduction to the three split procedures preg split(), explode(), and str split(). If you thought this Split Function in PHP blog was useful, check out Softlogic Systems‘ PHP Certification Training in Chennai and become an expert in Web Page Development.