This repository was archived by the owner on Aug 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_functions.php
More file actions
127 lines (108 loc) · 3.94 KB
/
string_functions.php
File metadata and controls
127 lines (108 loc) · 3.94 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
# substr()
# Returns a portion of a string
$output = substr('Hello', 1);
echo $output; // ello
echo '<br>';
$output = substr('Hello', 0);
echo $output; // Hello
echo '<br>';
$output = substr('Hello', 3);
echo $output; // lo
echo '<br>';
$output = substr('Hello', 1, 3); // start at 1, end at position 3
echo $output; // ell
echo '<br>';
// start from the back
$output = substr('Hello', -2); // start from 2(from the last)
echo $output; // lo
echo '<br>';
// length
$output = strlen('Hello World');
echo $output; // 11
echo '<br>';
# strpos()
# Finds the position of the first occurence of a sub string
$output = strpos('Hello World', 'o');
echo $output; // 4
echo '<br>';
# strrpos()
# Finds the position of the last occurence of a sub string
$output = strrpos('Hello World', 'o');
echo $output;
echo '<br>'; // 7
# trim()
# Strips whitespace
$text = 'Hello World ';
var_dump($text); // string(47) "Hello World "
echo '<br>';
$trimmedText = trim($text);
var_dump($trimmedText); // string(11) "Hello World"
echo '<br>';
# strtoupper
# Makes everything uppercase
$output = strtoupper('Hello World');
echo $output; // HELLO WORLD
echo '<br>';
# strtolower
# Makes everything lowercase
$output = strtolower('Hello World');
echo $output; // hello world
echo '<br>';
# ucwords()
# Capitalize every word
$output = ucwords('hello world');
echo $output; // Hello World
echo '<br>';
# str_replace()
# Replace all occurences of a search string with a replacement
$text = 'Hello World';
$output = str_replace('World', 'Everyone', $text);
echo $output; // Hello Everyone
echo '<br>';
# is_string()
# Check if string
$val = 'Hello';
$output = is_string($val);
echo $output; // 1
echo '<br>';
$val2 = 2;
$output = is_string($output);
echo $output; // We won't see anything but it actually returns false
echo '<br>';
$values = array(true, false, null, 'abc', 33, '33', 22.4, '22.4', '', ' ', 0, '0');
foreach($values as $value) {
if(is_string($value)) {
echo "{$value} is a string <br>";
}
}
/*
abc is a string
33 is a string
22.4 is a string
is a string
is a string
0 is a string
*/
# gzcompress()
# Compress a string
$string = "Lorem ipsum dolor sit amet consectetur adipisicing elit.
Voluptas officia id maiores debitis laudantium provident nisi placeat ducimus
delectus totam, unde quis cum necessitatibus minus suscipit praesentium explicabo
repellat autem. Quisquam adipisci animi aut temporibus nulla, quos voluptatem
possimus, inventore vero neque vitae molestias dolores? Accusamus delectus maiores quia
temporibus?";
$compressed = gzcompress($string);
echo $compressed; // unreadable but compressed text
echo '<br>';
/*
x�E�1n�0E��� A�.]�3S�DY$��?��z0,P~�}�ےN:=;Uk��5��.%$rW��Zt|�4�3���Ok9���vÔI+uV@��\5ԩqV�H��v�2�h4E{�����cXp?Q�*�%H�!E�zŝ�oO/��<��{6-|���dJk���g�rK�G���C��DŽ��#a$~:A���g]L�4�@�D:v�8���[��Bݚx(��ذ���JI�{翶�kCS>���KX�
*/
// uncompressing
$original = gzuncompress($compressed);
echo $original;
echo '<br>';
/*
Lorem ipsum dolor sit amet consectetur adipisicing elit. Voluptas officia id maiores debitis laudantium provident nisi placeat ducimus delectus totam, unde quis cum necessitatibus minus suscipit praesentium explicabo repellat autem. Quisquam adipisci animi aut temporibus nulla, quos voluptatem possimus, inventore vero neque vitae molestias dolores? Accusamus delectus maiores quia temporibus?
*/
?>