-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06_string_manupulations.php
124 lines (98 loc) · 2.68 KB
/
06_string_manupulations.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
<?php
/*
* Double quoted strings perform action on special characters (Variables).
* Single quoted strings does not perform such actions, it returns the string like it was written.
* All String Functions: https://www.w3schools.com/php/php_ref_string.asp
* */
// Difference between double quotes & single quotes.
$s = "dollars";
echo 'This costs a lot of $s.'; // This costs a lot of $s.
echo '<pre>';
echo "This costs a lot of $s."; // This costs a lot of dollars.
// String Length
echo '<pre>';
echo strlen("Hello world!");
// Word Count
echo '<pre>';
echo str_word_count("Hello world !@#$%^&*()"); // Cannot count special characters.
/*
* Search For Text Within a String
* The PHP strpos() function searches for a specific text within a string.
* If a match is found, the function returns the character position of the first match.
* If no match is found, it will return FALSE.
* The first character position in a string is 0 (not 1).
* */
$subject = "Hello world!";
echo '<pre>';
echo strpos($subject, "world");
// Upper Case
echo '<pre>';
echo strtoupper($subject);
// Lower Case
echo '<pre>';
echo strtolower($subject);
// Capitalize
echo '<pre>';
echo ucfirst($subject);
// Reverse
echo '<pre>';
echo strrev($subject);
// Replace
echo '<pre>';
echo str_replace("world", "PHP", $subject);
// Remove whitespace
$subject = " Hello world! ";
echo '<pre>';
echo trim($subject);
echo '<pre>';
echo rtrim($subject);
echo '<pre>';
echo ltrim($subject);
echo '<pre>';
echo trim(str_replace(' ', '', $subject));
/*
* Remove whitespaces using regular expressions perfectly.
* ^\s+ # Match whitespace at the start of the string
* | # or
* \s+$ # Match whitespace at the end of the string
* | # or
* \s+(?=\s) # Match whitespace if followed by another whitespace character
* */
echo '<pre>';
echo preg_replace('/^\s+|\s+$|\s+(?=\s)/', '', ' Hello world! ');
$cleanStr = trim(preg_replace('/\s\s+/', ' ', str_replace("\n", " ", $subject)));
echo '<pre>';
echo $cleanStr;
$subject = "Hello world!";
// Convert string into array
echo '<pre>';
print_r(explode(" ", $subject));
// Concatenation
echo '<pre>';
echo 'Hello ' . 'World!';
// Slicing
echo '<pre>';
echo substr($subject, 2, 5);
// Slice to the End
echo '<pre>';
echo substr($subject, 2);
// Slice from the end
echo '<pre>';
echo substr('4242 4242 4242 4242', -4);
// Negative length
echo '<pre>';
echo substr('4242 4242 4242 4242', 2, -2);
/*
* Escape characters
* \' Single Quote
* \" Double Quote
* \$ PHP variables
* \n New Line
* \r Carriage Return
* \t Tab
* \f Form Feed
* \ooo Octal value
* \xhh Hex value
* */
echo '<pre>';
echo "\"Love\" the life you live, live the life you \"love\".";