Pre-requisite
The scripts used in the examples of this tutorial have been written based on the PHP 8 version. Do the following task before executing the examples of this script.
- Install apache2 and PHP 8.
- Set execute permission for all files and folders under /var/www/html folder where all PHP files will be stored.
Split String Using Explode() Function
The explode() function is used to divide a string based on a particular separator value that is another string. The syntax of this function is given below.
Syntax:
The first two arguments of these functions are mandatory arguments and the third argument is optional. The first argument contains the delimiter value that is used to split the string. The second argument contains the main string that will be divided based on the delimiter. The third argument value is used to limit the number of splits that can be a positive or negative number. It returns an array of string values after dividing the string.
Use of Explode() Function
Different uses of explode() function without optional argument and with optional argument have shown in this part of this tutorial.
Example-1: Split the string based on space and store the values into an array
Create a PHP file with the following script to divide a string based on the space. Here, the explode() will return an array of three elements after dividing the string, “I like Programming” based on space. Next, the values of the array will be printed by using the index.
//Assign a string value
$string = "I like programming";
//Split the string into array based on space
$split_arr = explode(" ", $string);
//Print the main string value
echo "The string value is '$string'<br/>";
//Print the splitted values in each line
echo "<br/>The splitted values of the string are:<br/>";
echo $split_arr[0]."<br/>";
echo $split_arr[1]."<br/>";
echo $split_arr[2]."<br/>";
?>
Output:
The following output will appear after executing the above script. Here, the filename is split1.php that is stored inside /var/www/html/code folder.
http://localhost/code/split1.php
Example-2: Split the string with the limit value
Create a PHP file with the following script to divide a string based on the colon(:). Here, the first explode() function without limit will return an array of five elements after dividing the string, “‘HTML:CSS:JavaScript:PHP:MySQL” based on the colon.
The second explode() function with a positive limit value will return an array of three elements after dividing the same string. The third explode() function with a negative limit value will return an array of four elements after dividing the same string.
//Assign a string value
$courses = 'HTML:CSS:JavaScript:PHP:MySQL';
echo "<br />The splitted values without limit: <br />";
//Split the string based on colon without limit
$crs_arr = explode(':', $courses);
foreach ($crs_arr as $val)
{
echo $val."<br />";
}
echo "<br />The splitted values with positive limit: <br />";
//Split the string based on colon with positive limit
$crs_arr = explode(':', $courses, 3);
for ($i = 0; $i< 3; $i++)
{
echo $crs_arr[$i]."<br />";
}
echo "<br />The splitted values with negative limit: <br />";
//Split the string based on colon with negative limit
$crs_arr = explode(':', $courses, -1);
foreach ($crs_arr as $val)
{
echo $val."<br />";
}
?>
Output:
The following output will appear after executing the above script. Here, the filename is split2.php that is stored inside /var/www/html/code folder.
http://localhost/code/split2.php
Split String Using str_split() Function
The str_split() is another function to divide a string into the array without any separator like explode() function. The syntax of this function is given below.
Syntax:
The first argument of this function is mandatory argument and the second argument is optional. If the optional argument is omitted then the function will return an array of characters contains in the string. The optional argument is used to set the length of each element of the array.
Example-3: Split String Using str_split() Function
Create a PHP file with the following script to divide a string and store the values into an array. Here, the first str_split() function has been used without an optional argument that will create an array of characters. The second str_split() function has been used with the optional argument that will create an array of strings with the length of 7 characters.
//Assign a string value
$text = "Welcome Everyone";
//Split the string without length
echo "The splitted values without length:<br />";
$split_arr = str_split($text);
foreach ($split_arr as $val)
{
if($val == ' ')
{
echo "<br />";
continue;
}
echo $val;
}
//Split the string with length
echo "<br /><br />The splitted values with length:";
$split_arr = str_split($text, 7);
foreach ($split_arr as $val)
echo "<br />".$val;
?>
Output:
The following output will appear after executing the above script. Here, the filename is split3.php that is stored inside /var/www/html/code folder.
http://localhost/code/split3.php
Split String Using Preg_split() Function
The preg_split() function is used to divide string-based regular expression patterns. The syntax of this function is given below.
Syntax:
The first argument of this function contains the regular expression pattern that will use to divide the string. The second argument contains the string that will divide. The optional argument limit is used to set the limit of the substring. The optional argument flag is used to set the return value in multiple ways.
Example-4: Split String Using Preg_split() Function
Create a PHP file with the following script to divide a string based on a pattern and store the values into an array. Here, the first preg_split() function has been used without any optional argument that will create an array of two elements by splitting the string, “Hello000 World” with the pattern, “[0-9]+”.
//Assign the string value
$text = "Hello000 World";
echo "The main string:<br/>$text";
echo "<br/><br/>The splitted string values:<br/>";
//Split the string based on pattern
$split_arr = preg_split("[0-9]+", $text);
//Print the splitted values
foreach ($split_arr as $val)
echo $val;
?>
Output:
The following output will appear after executing the above script. Here, the filename is split4.php that is stored inside /var/www/html/code folder.
http://localhost/code/split4.php
Conclusion
Three different ways to split strings in PHP have been explained in this tutorial by using multiple examples for helping the new PHP users to split the string easily by using PHP script.