Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

26.

3 String Processing and Regular


Expressions

String processing

Equality and comparison two important


operations
strcmp function

Returns 1 if string 1 < string 2


Returns 0 if string 1 = string 2
Returns 1 if string 1 > string 2

Relational operators

1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"


2

"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)

7 <html xmlns = "http://www.w3.org/1999/xhtml">


8
9
10

<head>
<title>String Comparison</title>
</head>

11
12
13

<body>
<?php

Use a for loop to iterate through each array element.

14
15

// create array fruits

16

$fruits = array( "apple", "orange", "banana" );

17
18

// iterate through

19

for ( $i = 0; $i <

20

Function strcmp compares two strings. If the first string


alphabetically precedes the second, then 1 is returned. If
each array element
the strings are equal, 0 is returned. If the first string
count( $fruits ); $i++ ) {
alphabetically follows the second, then 1 is returned.

21

// call function strcmp to compare the array element

22

// to string "banana"

23

if ( strcmp( $fruits[ $i ], "banana" ) < 0 )

24

print( $fruits[ $i ]." is less than banana " );

elseif ( strcmp( $fruits[ $i ], "banana" ) > 0 )

25

print( $fruits[ $i ].

26

" is greater than banana " );

27
else

28

print( $fruits[ $i ]." isUse


equarelational
l to bananoperators
a " );

to compare each array


compare.php
element to string apple.
(2 of 2)

29
30
31

// use relational operators to compare each element

32

// to string "apple"

33

if ( $fruits[ $i ] < "apple" )


print( "and less than apple! <br />" );

34

elseif ( $fruits[ $i ] > "apple" )

35

print( "and greater than apple! <br />" );

36

elseif ( $fruits[ $i ] == "apple" )

37

print( "and equal to apple! <br />" );

38
39
}

40
41
42

?>
</body>

43 </html>

26.3 String Processing and Regular


Expressions
Fig. 26.7

Using the string comparison operators.

26.3 String Processing and Regular


Expressions

Regular expressions

Pattern matching templates


ereg function

POSIX

preg_match function

Perl

ereg_replace function

Building regular expressions

Metacharacters
$, ., ^

Brackets [

]
5

1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"


2

"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)

7 <html xmlns = "http://www.w3.org/1999/xhtml">


8
9
10

<head>
<title>Regular expressions</title>
</head>

11
12

<body>

14

Function ereg searches for the literal


characters Now inside variable $search.
$search = "Now is the time";

15

print( "Test string is: '$search'<br /><br />" );

13

<?php

16
17

// call function ereg to search for pattern 'Now'

18

// in variable search

19

if ( ereg( "Now", $search ) )

20

print( "String 'Now' was found.<br />" );

21

22

// search for pattern 'Now' in the beginning of

23

// the string

24

if ( ereg( "^Now", $search ) )

The dollar sign special character ($) search for the


pattern
Now at
end of the
the string.
The caret special
character
(^)the
matches
expression.php
of the line.<br />" );
beginning of a string. Function ereg searches the
(2 of 3)
beginning
of
the
string
for
pattern
Now
.
search for pattern 'Now' at the end of the string
print( "String 'Now' found at beginning

25
26
27
28

//

29

if ( ereg( "Now$", $search ) )

30
31

print( "String 'Now' was found at the end


of the line.<br />" );

32
33

// search for any word ending in 'ow'

34

if ( ereg( "[[:<:]]([a-zA-Z]*ow)[[:>:]]", $search,

35

$match ) )

36

print( "Word found ending in 'ow': " .

37

$match[ 1 ] . "<br />" );

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

// search for any words beginning with 't'

40

print( "Words beginning

41
42

while ( eregi( "[[:<:]](t[[:alpha:]]+)[[:>:]]",

43

$search, $match ) ) {

44

print( $match[ 1 ] . " " );

45

The pattern used in this example,

[[:<:]](t[[:alpha:]]+)[[:>:]], matches any


The while word
loop is
used to find
occurrence of a
beginning
witheach
the character
Function eregi
is used
to specify
case
insensitivet followed by one or
7
word in themore
stringcharacters.
beginningCharacter
with t. class [[:alpha:]]
pattern matches.

recognizes any alphabetic character.

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.

26.3 String Processing and Regular


Expressions
Fig. 26.8

Regular expressions in PHP.

26.3 String Processing and Regular


Expressions
Q
u
a
n
tifie
r

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

26.3 String Processing and Regular


Expressions
C
h
a
r
a
c
te
rc
la
s
s D
e
s
c
r
ip
tio
n
a
l
n
u
m
a
l
p
h
a
d
i
g
i
t
s
p
a
c
e
l
o
w
e
r
u
p
p
e
r

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

26.4 Viewing Client/Server


Environment Variables

Environment variables

Provide information about execution


environment

Type of Web browser


Type of server
Details of HTTP connection

Stored as array in PHP


$_ENV

12

You might also like