Problem:
You have a long string that consists of some lines (see the following code). Each line consists of information of each employee and lines are separated by new line characters. Now, we would like to separate each employee information in different variable so that we can use it according to our needs.
<?php $employees = "Name: Keith B. Covey Designation: Project Co-ordinator Height: 187 centimeters. Name: John C. Evans Designation: Cheif Operating Officer Height: 179 centimeters. Name: Ernest K. Peterson Designation: Senior Marketing Executive Height: 180 centimeters. Name: Daniel M. Beal Designation: Human Resource Manager Height: 182 centimeters."; ?>
Solution:
Use explode() function in php.
Example:
<?php $employees = "Name: Keith B. Covey Designation: Project Co-ordinator Height: 187 centimeters. Name: John C. Evans Designation: Cheif Operating Officer Height: 179 centimeters. Name: Ernest K. Peterson Designation: Senior Marketing Executive Height: 180 centimeters. Name: Daniel M. Beal Designation: Human Resource Manager Height: 182 centimeters."; $employee_arr = explode(PHP_EOL, $employees); foreach ($employee_arr as $employee){ echo "<br />---------------------------------------------------------------------------------------------<br />"; echo $employee; } ?>
[wpdm_file id=11]
Output:
———————————————————————————————
Name: Keith B. Covey Designation: Project Co-ordinator Height: 187 centimeters.
———————————————————————————————
Name: John C. Evans Designation: Cheif Operating Officer Height: 179 centimeters.
———————————————————————————————
Name: Ernest K. Peterson Designation: Senior Marketing Executive Height: 180 centimeters.
———————————————————————————————
Name: Daniel M. Beal Designation: Human Resource Manager Height: 182 centimeters.
———————————————————————————————
How it works:
Line 1 to 6 | Variable $employees holds a long text which consists of information of all employees. |
Line 7 | The explode function splits the content of variable $employees by PHP_EOL which is a PHP constant that indicates end of a line. In the $employees variable, after each employee information, the line ends and new employee information starts in the nest line. After splitting, the explode function returns an array. We define that array as $employee_arr. Each element of the array $employee_arr stores information of each employee. |
Line 8 to 11 | The foreach loop prints each employee information as separate line. |
explode() function at a glance
(PHP4, PHP5)
Usages:
explode() function splits up a string into an array.
Syntax:
explode(separator, string, limit)
Parameters:
Parameter | What it does | |
separator | Required | Specify where to break the string |
string | Required | Specify the string to split |
limit | Optional | Specify the number of array element to return |
- If the limit is a positive number, the returned array will contain a maximum of limit element with the last element containing the rest of string.
- If the limit is zero, it is equivalent to 1.
- If the limit is negative, it removes the last limit elements and returns the array.
Example 1: How to retrieve minute from 10:30:12
<?php $time = "10:30:12"; $time_arr = explode(":", $time); $minute = $time_arr[1]; echo "Minutes = ". $minute; ?>
[wpdm_file id=12]
Output:
Minute = 30
Explanation:
Line 3 | >The explode function splitting up the variable $time by “:” generating 10, 30, and 12. Then, it stored those values in $time_arr |
Line 4 | As minute is in the second element(10:30:12), so, it is stored in the second element of the $time_arr array. That is $time_arr[1]. |
Line 5 | Printing the minute |
Example 2: Use of optional parameter
<?php $languages = "PHP MySQL JavaScript jQuery CSS HTML"; $languages_arr = explode(" ", $languages, 4); foreach ($languages_arr as $language){ echo "<br />"; echo $language; } echo "<br /> -------------"; $languages_arr = explode(" ", $languages, 0); foreach ($languages_arr as $language){ echo "<br />"; echo $language; } echo "<br /> -------------"; $languages_arr = explode(" ", $languages, -2); foreach ($languages_arr as $language){ echo "<br />"; echo $language; } ?>
[wpdm_file id=13]
Output:
PHP
MySQL
JavaScript
jQuery CSS HTML
————-
PHP MySQL JavaScript jQuery CSS HTML
————-
PHP
MySQL
JavaScript
jQuery
Explanation:
Line 4 | Assigning 4 in the third optional parameter, the explode function returns an array with 4 elements, the fourth element(jQuery) contains the remaining part(CSS HTML) of the string |
Line 12 | In the third parameter 0 is equivalent to 1. Assigning 0, the explode function returns an array with one element (PHP) and add the remaining part of the string( MySQL JavaScript jQuery CSS HTML) with this. |
Line 20 | Assigning -2 in the third optional parameter, the explode function returns an array with all the elements except the last 2 elements (CSS HTML). |