-
Notifications
You must be signed in to change notification settings - Fork 0
/
explode.php
98 lines (98 loc) · 1.42 KB
/
explode.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
function ft_strlen($str)
{
$tmp = "";
$i = 0;
while ($tmp != $str) {
$tmp .= $str[$i];
$i++;
}
return $i;
}
function ft_trim($str)
{
$i = 0;
$new_str = "";
$flag = true;
$j = ft_strlen($str) -1;
while ($j > 0) {
if($str[$j] != " ")
{
$j++;
break;
}
else
$j--;
}
while ($i < $j) {
if($str[$i] != " ")
{
$flag = false;
}
if(!$flag)
{
$new_str .= $str[$i];
}
$i++;
}
return $new_str;
}
function ft_count($tab)
{
$i = 0;
foreach($tab as $value) {
$i++;
}
return $i;
}
function ft_array_push(&$tab,$new_elem)
{
$i = ft_count($tab);
$tab[$i] = $new_elem;
}
function ft_explode($delimiter,$str)
{
$i = 0;
$j = 0;
$test = false;
$tmp = "";
$array = array();
if(ft_strlen($delimiter) == 0)
{
echo "PHP Warning: ft_explode(): Empty delimiter\n";
return;
}
while($i < ft_strlen($str))
{
if($str[$i] == $delimiter)
{
$test = true;
$str[$i] = "";
while ($j <= $i) {
$tmp.=$str[$j];
$j++;
}
$tmp = ft_trim($tmp);
if($tmp !== $delimiter)
ft_array_push($array, $tmp);
$tmp = "";
}
$i++;
}
if($test)
{
while ($j < ft_strlen($str)) {
$tmp.=$str[$j];
$j++;
}
$tmp = ft_trim($tmp);
if($tmp !==$delimiter)
ft_array_push($array, $tmp);
}
if(!$test)
{
ft_array_push($array, $str);
}
return $array;
}
?>