Tuesday, 14 August 2018

How to Break up a Text Into Separate Lines In PHP?

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.
1
2
3
4
5
6
<?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:
1
2
3
4
5
6
7
8
9
10
11
<?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;
}
?>
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 6Variable $employees holds a long text which consists of information of all employees.
Line 7The 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 11The 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:
ParameterWhat it does
separatorRequiredSpecify where to break the string
stringRequiredSpecify the string to split
limitOptionalSpecify 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

1
2
3
4
5
6
<?php
$time = "10:30:12";
$time_arr = explode(":", $time);
$minute = $time_arr[1];
echo "Minutes = ". $minute;
?>
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 4As 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 5Printing the minute

Example 2:  Use of optional parameter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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;
 }
?>
Output:PHP
MySQL
JavaScript
jQuery CSS HTML
————-
PHP MySQL JavaScript jQuery CSS HTML
————-
PHP
MySQL
JavaScript
jQuery
Explanation:
Line 4Assigning 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 12In 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 20Assigning -2 in the third optional parameter, the explode function returns an array with all the elements except the last 2 elements (CSS HTML).

0 comments:

Post a Comment