/
stringFunctions.php
199 lines (126 loc) · 5.55 KB
/
stringFunctions.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
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
<?php
// Commonly used string functions in PHP
/*
Here is a list of 20 commonly used string functions in PHP:
strlen — Get string length
substr — Return part of a string
strpos — Find the position of the first occurrence of a substring in a string
str_replace — Replace all occurrences of the search string with the replacement string
trim — remove whitespace
strtoupper — Make a string uppercase
strtolower — Make a string lowercase
ucfirst — Make a string's first character uppercase
ucwords — Uppercase the first character of each word in a string
strrev — Reverse a string
str_pad — Pad a string to a certain length with another string
str_repeat — Repeat a string
str_split — Convert each word of a string to an array
explode — Split a string by a string
implode — Join array elements with a string
addslashes
stripslashes
htmlentities
html_entity_decode
strip_tags
======================
These functions can be used to perform various operations on strings such as finding the length of a string, extracting substrings, finding and replacing substrings, trimming whitespaces, converting cases, reversing strings, padding strings, splitting strings, converting arrays to strings and vice versa, encoding and decoding HTML entities, and removing HTML tags.
=============================
*/
//===== strlen: Returns the length of a string.
// $string = "Hello, World!";
// $length = strlen($string);
// echo "The length of the string is: $length";
//===== strpos: Returns the position of the first occurrence of a substring in a string.
// $string = "Hello, World!";
// $position = strpos($string, "World");
// echo "The position of the first occurrence of 'World' is: $position";
//===== str_replace: Replaces all occurrences of a search string with a replacement string.
// $string = "Hello, World!";
// $new_string = str_replace("World", "PHP", $string);
// echo "The new string is: $new_string";
//===== substr: Returns a portion of a string.
// $string = "Hello, World!";
// $portion = substr($string, 7);
// echo "The portion of the string is: $portion";
//===== trim: Removes whitespaces or specified characters from the beginning and end of a string.
// $string = " Hello, World! ";
// $trimmed = trim($string);
// echo "The trimmed string is: $trimmed";
//===== strtolower: Converts a string to lowercase.
// $string = "Hello, World!";
// $lower = strtolower($string);
// echo "The lowercase string is: $lower";
//===== strtoupper: Converts a string to uppercase.
// $string = "Hello, World!";
// $upper = strtoupper($string);
// echo "The uppercase string is: $upper";
//===== explode: Splits a string into an array based on a specified delimiter.
// $string = "apple,banana,cherry";
// $fruits = explode(",", $string);
// print_r($fruits);
//===== implode: Joins elements of an array into a string using a specified glue.
// $fruits = array("apple", "banana", "cherry");
// $string = implode(",", $fruits);
// echo "The string is: $string";
//===== str_split: Converts a string into an array of individual characters.
// $string = "Hello, World!";
// $characters = str_split($string);
// print_r($characters);
//===== md5: Calculates the MD5 hash of a string.
// $string = "Hello, World!";
// $hash = md5($string);
// echo "The hash is: $hash";
//===== sha1: Calculates the SHA1 hash of a string.
// $string = "Hello, World!";
// $hash = sha1($string);
// echo "The hash is: $hash";
//===== ucfirst: This function capitalizes the first character of a string.
// $string = "hello world";
// echo ucfirst($string);
//Output: Hello world
//===== ucwords: This function capitalizes the first character of each word in a string.
// $string = "hello world";
// echo ucwords($string);
// Output: Hello World
//===== strrev: This function reverses a string.
// $string = "hello";
// echo strrev($string);
// Output: olleh
//===== str_pad: This function pads a string to a certain length with another string.
// $string = "hello";
// echo str_pad($string, 10, "-=", STR_PAD_BOTH);
// Output: -=-hello-=-
//===== str_repeat: This function repeats a string a specified number of times.
// $string = "hello";
// echo str_repeat($string, 3);
// Output: hellohellohello
//===== str_split: This function splits a string into an array of substrings.
// $string = "hello";
// $array = str_split($string, 2);
// print_r($array);
// Output: Array ( [0] => he [1] => ll [2] => oo )
//===== addslashes: This function adds backslashes to a string. It can be used to escape characters in a string that have a special meaning in PHP, such as quotes and backslashes.
// $string = "I'm a developer";
// echo addslashes($string);
// Output: I\'m a developer
//===== stripslashes: This function removes backslashes from a string that were added by the addslashes function.
// $string = "I\'m a developer";
// echo stripslashes($string);
// Output: I'm a developer
//===== htmlentities: This function converts characters to their corresponding HTML entities.
// $string = "<p>Hello, World!</p>";
// echo htmlentities($string);
// Output: <p>Hello, World!</p>
//===== html_entity_decode: This function converts HTML entities to their corresponding characters.
// $string = "<p>Hello, World!</p>";
// echo html_entity_decode($string);
// Output: <p>Hello, World!</p>
//===== strip_tags: This function removes HTML and PHP tags from a string.
// $string = "<p>Hello, World!</p>";
// echo strip_tags($string);
// Output: Hello, World!
//===== wordwrap()
// $string = "Hello World!";
// echo wordwrap($string,3,"\n");
// number_format()
echo number_format("1000000",2)."\n";