-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathregex.php
169 lines (145 loc) · 6.54 KB
/
regex.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
<?php
// Start the session
session_start();
?>
<!doctype html>
<html lang="en">
<head>
<?php require_once "_header.php" ?>
<title>RegExp - Regular Expression</title>
</head>
<body>
<?php require_once "_navbar.php" ?>
<div class="container">
<div class="row justify-content-center mt-5">
<div class="col-12 col-sm-6 align-self-center">
<div class="card">
<div class="card-body text-center">
<h1>RegExp - Regular Expression</h1>
<hr>
<h3>
preg_match()
</h3>
<hr>
<?php
$string = "www.nasirkhn.com";
$pattern = "/nasir/";
if (preg_match($pattern, $string))
{
echo "The url '$string' contains 'nasir'";
}
else
{
echo "The url '$string' does not contain 'nasir'";
}
?>
<hr>
<?php
$pattern = "/ca[kf]e/";
$text = "He was eating cake in the cafe.";
echo "Pattern: $pattern <br>";
echo "Text: $text <br>";
if(preg_match($pattern, $text)){
echo "Match found!";
} else{
echo "Match not found.";
}
?>
<hr>
<h3>
preg_split()
</h3>
<hr>
<?php
$string = "I Love Regular Expressions";
$pattern = "/ /";
echo "String: $string <br>";
$return_array = preg_split($pattern, $string);
print_r($return_array );
?>
<hr>
<?php
$pattern = "/[\s,]+/";
$text = "My favourite colors are red, green and blue";
echo "String: $text <br>";
$parts = preg_split($pattern, $text);
// Loop through parts array and display substrings
foreach($parts as $part){
echo $part . "<br>";
}
?>
<hr>
<h3>
preg_replace()
</h3>
<hr>
<?php
$string = "My website address is nasirkhn.com";
$pattern = "/nasirkhn.com/";
$replacement = "http://nasirkhn.com";
echo "String: $string <br>";
$string_new = preg_replace($pattern, $replacement, $string);
echo "$string_new";
?>
<hr>
<?php
$pattern = "/\s/";
$replacement = "-";
$text = "Earth revolves around\nthe\tSun";
echo "Pattern: $pattern <br>";
echo "Text: <pre>$text</pre> <br>";
// Replace spaces, newlines and tabs
$text_new = preg_replace($pattern, $replacement, $text);
echo "Text: <pre>$text_new</pre> <br>";
// Replace only spaces
$text_new = str_replace(" ", "-", $text);
echo "Text: <pre>$text_new</pre> <br>";
?>
<hr>
<?php
$pattern = '/\bcar\w*/';
$replacement = '<b>$0</b>';
$text = 'Words begining with car: cart, carrot, cartoon. Words ending with car: scar, oscar, supercar.';
echo "Pattern: $pattern <br>";
echo "Text: <pre>$text</pre> <br>";
$text_new = preg_replace($pattern, $replacement, $text);
echo "New Text: <pre>$text_new</pre> <br>";
?>
<hr>
<h3>
preg_grep()
</h3>
<hr>
<?php
$pattern = "/^B/";
$names = array("Bangladesh", "Nepal", "Bhutan");
echo "Names: ";
print_r($names);
$matches = preg_grep($pattern, $names);
// Loop through matches array and display matched names
echo "<br><br>Names starts with B<br>";
foreach($matches as $match){
echo $match . "<br>";
}
?>
<hr>
<h3>
preg_match_all()
</h3>
<hr>
<?php
$pattern = "/color/i";
$text = "Color red is more visible than color blue in daylight.";
echo "Pattern: $pattern <br>";
echo "Text: $text <br>";
$matches = preg_match_all($pattern, $text, $array);
echo $matches . " matches were found.";
?>
</div>
</div>
</div>
</div>
</div>
<?php include_once "_footer.php" ?>
</body>
</html>