-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.php
More file actions
261 lines (231 loc) · 7.41 KB
/
string.php
File metadata and controls
261 lines (231 loc) · 7.41 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<html>
<head>
</head>
<body>
<?php
//using add slashes
echo 'Add Slashes Method:<br>String before the Add Slashes Method: ';
$str = "Is your name O'Reilly?";
echo "$str" . '<br>String after the Add Slashes Method: ';
// Outputs: Is your name O\'Reilly?
echo addslashes($str);
//using chr
echo '<br><br>Chr Method:<br>The ASCII value for the character we will use is: ';
$val = 47;
echo "$val" . '<br>';
$str = "The string ends in a slash: ";
$str .= chr($val); /* add an slash character at the end of $str */
echo "$str";
// using chunk split
echo '<br><br>Chunk Split Method:';
echo '<br>';
$data = 'Hey. My name is John. I like food. Food is good. Yum.';
echo "$data";
echo '<br>';
$new_string = chunk_split($data, "4");
echo "$new_string";
//using count chars
echo '<br><br>';
echo 'Count Chars Method:';
echo '<br>';
$data2 = 'Two Ts and one F.';
echo "$data2";
echo '<br>';
foreach (count_chars($data2, 1) as $i => $val) {
echo "There were $val instance(s) of \"" , chr($i) , "\" in the string.";
echo '<br>';
}
//using echo
echo '<br><br>Echo Method:<br>Echoing a string prints the string.<br>Example: ';
echo 'This is an echoed string';
//using explode
echo '<br><br>';
echo 'Explode Method:';
echo '<br>';
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
echo "$pizza";
echo '<br>';
$pieces = explode(" ", $pizza);//separate the pizza string by white space and store each substring as an element in an array
echo 'First 2 elements in the array after exploding the above string:';
echo '<br>Pieces[0]: ';
echo $pieces[0]; // piece1
echo '<br>Pieces[1]: ';
echo $pieces[1]; // piece2
//using html entities
echo '<br><br>HTML Entities Method:<br>';
$str = "A 'quote' is <b>bold</b>";
echo "$str";
echo '<br>';
// Outputs: A 'quote' is <b>bold</b>
echo htmlentities($str);
echo '<br>';
// Outputs: A 'quote' is <b>bold</b>
echo htmlentities($str, ENT_QUOTES);
//using html special chars decode
echo '<br><br>HTML Special Chars Decode Method:<br>';
$str2 = "<p>this -> "</p>\n";
echo htmlspecialchars_decode($str2);
// note that here the quotes aren't converted
echo htmlspecialchars_decode($str2, ENT_NOQUOTES);
//using html special chars
echo '<br><br>HTML Special Chars Method:<br>';
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // <a href='test'>Test</a>
//using implode
echo '<br><br>Implode Method:<br>';
$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
echo $comma_separated; // lastname,email,phone
//using lcfirst
echo '<br><br>LCFirst Method:<br>Before lcfirst method: ';
$foo = 'HelloWorld';
echo "$foo";
echo '<br>After lcfirst method: ';
$foo = lcfirst($foo); // helloWorld
echo "$foo";
echo '<br>Before lcfirst method: ';
$bar = 'HELLO WORLD!';
echo "$bar";
echo '<br>After lcfirst method: ';
$bar = lcfirst($bar); // hELLO WORLD!
echo "$bar";
//using ltrim
echo '<br><br>LTrim Method:<br>Before LTrim method:<br>';
$text = "\t\tThese are a few words :) ... ";
$hello = "Hello World";
echo "$text" . '<br>' . "$hello" . '<br>';
echo 'After LTrim method: <br>';
$trimmed = ltrim($text);
echo "$trimmed";
echo '<br>';
$trimmed = ltrim($text, " \t.");
echo "$trimmed";
echo '<br>';
$trimmed = ltrim($hello, "Hdle");
echo "$trimmed";
//using md5
echo '<br><br>md5 Method:<br>';
$str = 'apple';
echo "$str";
echo '<br>';
$hash = md5($str);
echo 'The hash value of the string "apple" is: ' . "$hash" . '<br>';
if (md5($str) === '1f3870be274f6c49b3e31a0c6728957f') {
echo "Would you like a green or red apple?";
}
//using rtrim
echo '<br><br>RTrim Method:<br>Before RTrim method:<br>';
$text = "\t\tThese are a few words :) ... ";
$hello = "Hello World";
echo "$text" . '<br>' . "$hello" . '<br>';
echo 'After RTrim method: <br>';
$trimmed = rtrim($text);
echo "$trimmed";
echo '<br>';
$trimmed = rtrim($text, " \t.");
echo "$trimmed";
echo '<br>';
$trimmed = rtrim($hello, "Hdle");
echo "$trimmed";
//using str getcsv
echo '<br><br>Str GetCSV Method:<br>';
$csv = array_map('str_getcsv', file('contacts.csv'));
print_r($csv);
//using strip_tags
echo '<br><br>Strip Tags Method:<br>';
$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo 'The string before tags are stripped:<br>';
echo "$text";
echo '<br>The string after tags are stripped:<br>';
echo strip_tags($text);
echo '<br>The string after all tags are stripped excluding the a tag: ';
// Allow <p>
echo strip_tags($text, '<a>');
//using strreplace
// Provides: You should eat pizza, beer, and ice cream every day
echo '<br><br>StrReplace Method:<br>Before StrReplace Method: ';
$phrase = "You should eat fruits, vegetables, and fiber every day.";
echo "$phrase";
$healthy = array("fruits", "vegetables", "fiber");
$yummy = array("pizza", "beer", "ice cream");
$newphrase = str_replace($healthy, $yummy, $phrase);
echo '<br>After StrReplace Method: ' . "$newphrase";
//using str split
echo '<br><br>Str Split Method:<br>Before Str Split Method: ';
$str = "Hello Friend";
echo "$str";
$arr1 = str_split($str);
$arr2 = str_split($str, 3);
echo '<br>Array after splitting string into elements 1 character long: <br>';
print_r($arr1);
echo '<br>Array after splitting string into elements up to 3 character long: <br>';
print_r($arr2);
//using stripos
echo '<br><br>StrIPos Method:<br>';
$findme = 'a';
$mystring1 = 'xyz';
$mystring2 = 'ABC';
$pos1 = stripos($mystring1, $findme);
$pos2 = stripos($mystring2, $findme);
// Nope, 'a' is certainly not in 'xyz'
echo 'Searching for the string "a" in the string "xyz": ';
if ($pos1 === false) {
echo "The string '$findme' was not found in the string '$mystring1'";
}
// Note our use of ===. Simply == would not work as expected
// because the position of 'a' is the 0th (first) character.
echo '<br>Searching for the string "a" in the string "ABC": ';
if ($pos2 !== false) {
echo "We found '$findme' in '$mystring2' at position $pos2";
}
//using strlen
echo '<br><br>Strlen Method:<br>The string is: ';
$str = 'abcdef';
echo "$str";
echo '<br>The length of the string is: ';
echo strlen($str); // 6
//using strpos
echo '<br><br>StrPos Method:<br>';
$findme = 'a';
$mystring1 = 'ABC';
$mystring2 = 'abc';
$pos1 = strpos($mystring1, $findme);
$pos2 = strpos($mystring2, $findme);
// Nope, 'a' is certainly not in 'xyz'
echo 'Searching for the string "a" in the string "ABC": ';
if ($pos1 === false) {
echo "The string '$findme' was not found in the string '$mystring1'";
}
// Note our use of ===. Simply == would not work as expected
// because the position of 'a' is the 0th (first) character.
echo '<br>Searching for the string "a" in the string "abc": ';
if ($pos2 !== false) {
echo "We found '$findme' in '$mystring2' at position $pos2";
}
//using strtolower
echo '<br><br>Str To Lower Method:<br>Before the Str To Lower method: ';
$str = "Mary Had A Little Lamb and She LOVED It So";
echo "$str";
$str = strtolower($str);
echo '<br>After the Str To Lower method: ';
echo $str; // Prints mary had a little lamb and she loved it so
//using trim
echo '<br><br>Trim Method:<br>Before Trim method:<br>';
$text = "\t\tThese are a few words :) ... ";
$hello = "Hello World";
echo "$text" . '<br>' . "$hello" . '<br>';
echo 'After Trim method: <br>';
$trimmed = trim($text);
echo "$trimmed";
echo '<br>';
$trimmed = trim($text, " \t.");
echo "$trimmed";
echo '<br>';
$trimmed = trim($hello, "Hdle");
echo "$trimmed";
echo '<br>';
$trimmed = trim($hello, 'HdWr');
echo "$trimmed";
?>
</body>
</html>