-
Notifications
You must be signed in to change notification settings - Fork 0
/
explode_implode.php
58 lines (25 loc) · 1.12 KB
/
explode_implode.php
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
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
$physicists_string = 'Gordon Freeman, Samantha Carter, Sheldon Cooper, Quinn Mallory, Bruce Banner, Tony Stark';
echo $physicists_string . PHP_EOL;
$now_array = explode(', ', $physicists_string);
print_r($now_array) . PHP_EOL;
//============================================================
$physicists_array = ['Gordon Freeman', 'Samantha Carter', 'Sheldon Cooper', 'Quinn Mallory', 'Bruce Banner', 'Tony Stark'];
print_r($physicists_array);
$new_string = implode('|', $physicists_array);
echo $new_string . PHP_EOL;
//=============================================================
function humanized_list($string, $sort = false)
{
$array = explode(', ', $string);
if ($sort == 'sort')
{
sort($array);
}
$last_item = array_pop($array);
$final_string = implode(',', $array);
return $final_string . ' and ' . $last_item;
}
$physicists_string = 'Gordon Freeman, Samantha Carter, Sheldon Cooper, Quinn Mallory, Bruce Banner, Tony Stark';
$famous_fake_physicists = humanized_list($physicists_string, sort);
echo "Some of the most famous fictional theoretical physicists are $famous_fake_physicists.\n";