Professional Documents
Culture Documents
SynapseIndia PHP Development String Processing and Regular Expressions
SynapseIndia PHP Development String Processing and Regular Expressions
String processing
Relational operators
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
4 <!-- Fig. 26.7: compare.php -->
5 <!-- String Comparison
-->
compare.php
(1 of 2)
<head>
<title>String Comparison</title>
</head>
11
12
13
<body>
<?php
14
15
16
17
18
// iterate through
19
for ( $i = 0; $i <
20
21
22
// to string "banana"
23
24
25
print( $fruits[ $i ].
26
27
else
28
29
30
31
32
// to string "apple"
33
34
35
36
37
38
39
}
40
41
42
?>
</body>
43 </html>
Regular expressions
POSIX
preg_match function
Perl
ereg_replace function
Metacharacters
$, ., ^
Brackets [
]
5
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
4 <!-- Fig. 26.8: expression.php -->
5 <!-- Using regular expressions -->
6
expression.php
(1 of 3)
<head>
<title>Regular expressions</title>
</head>
11
12
<body>
14
15
13
<?php
16
17
18
// in variable search
19
20
21
22
23
// the string
24
25
26
27
28
//
29
30
31
32
33
34
35
$match ) )
36
37
38
The expression
the expressions
parentheses,[[:<:]]
[a-zA-Z]*ow
The specialinside
bracket
and ,
matches
any
word
ending
in ow. The
quantifier
Placing
a pattern
in the
parentheses
stores
the of
matched
[[:>:]]
match
beginning
and
end
a*
with 't' found: ");
matches
thethe
preceding
pattern
0 or more
times.
string
in
array that
is specified
in the
third argument
word,
respectively.
to function ereg.
39
40
41
42
43
$search, $match ) ) {
44
45
4
6
/
/
r
e
m
o
v
e
t
h
e
f
i
r
s
t
o
c
c
u
r
r
e
n
c
e
o
f
a
w
o
r
d
b
e
g
i
n
n
i
n
g
4
7
/
/
w
i
t
h
'
t
'
t
o
f
i
n
d
o
t
h
e
r
i
n
s
t
a
n
c
e
s
i
n
t
h
e
s
t
r
i
n
g
4
8
$
s
e
a
r
c
h
=
e
r
e
g
_
r
e
p
l
a
c
e
(
$
m
a
t
c
h
[
1
]
,
"
"
,
$
s
e
a
r
c
h
)
;
}
4
9
5
0
5
1
5
2
5
3
p
r
i
n
t
(
"
<
b
r
/
>
"
)
;
?
>
<
/
b
o
d
y
>
5
4<
/
h
t
m
l
>
expression.php
After printing a match of a word beginning with t, function
(3 of
3)
ereg_replace is called to remove the word from the
string.
This is necessary be because to find multiple instances of a
given pattern, the first matched instance must first be removed.
Function ereg_replace takes three arguments: the pattern to
match, a string to replace the matched string and the string to
search.
M
a
tc
h
e
s
{
n
}
{
m
,
n
}
{
n
,
}
+
*
?
E
x
a
c
tlyn
tim
e
s
.
B
e
tw
e
e
nm
a
n
dn
tim
e
sin
c
lu
s
iv
e
.
n
o
rm
o
retim
e
s
.
O
n
eo
rm
o
retim
e
s(s
a
m
ea
s{
1
,
}
).
Z
e
roo
rm
o
retim
e
s(s
a
m
ea
s{
0
,
}
).
Z
e
roo
ro
n
etim
e(s
a
m
ea
s{
0
,
1
}
).
F
ig
.2
6
.9
S
o
m
e
P
H
P
q
u
a
n
tifie
rs
.
10
A
lp
h
a
n
u
m
e
r
ic
c
h
a
r
a
c
te
r
s(
i.e
.,le
tte
r
s[
a
z
A
Z
]o
rd
ig
its[
0
9
]
)
.
W
o
r
d
c
h
a
r
a
c
te
r
s(
i.e
.,le
tte
r
s[
a
z
A
Z
]
)
.
D
ig
its
.
W
h
ite
s
p
a
c
e
.
L
o
w
e
r
c
a
s
e
le
tte
r
s
.
U
p
p
e
r
c
a
s
e
le
tte
r
s
.
F
ig
.2
6
.1
0 S
o
m
e
P
H
P
c
h
a
r
a
c
te
rc
la
s
s
e
s
.
11
Environment variables
12