Figure 6.39. Viewing the source page in the browser.
%
%
%
The htmlentities() Function
The htmlentities() function converts all applicable characters to their HTML entities equivalents.
Format
string htmlentities (string string [, int quote_style [,
string charset]])
%
Example:
$string = "5¢ won't get you much at the café Française!"; echo
htmlentities($string, ENT_COMPAT); // Returns: 5¢ won't get you much at the
cafè
Française!
Table 6.17. Supported Character Sets
Charset
Aliases
Description
IBUO[[bcO
!
IBU[[bcO!
?$)*$(,%_7(06$.,8%^.*',O!4
IBUO[[bcO
!b
IBU[[bcO!b
?$)*$(,%_7(06$.,8%^.*',Oc4%W )%*#$%_7(0%)'/,8%C($,3#%
.,-%C',,')#%+$**$()%<'))',/%',%^.*',O!%JIBUO[[bcO!K4
X"CO[
%
WBSIIO30<6.*'2+$%<7+*'2=*$%[O2'*%X,'30-$4
36[DD
'2<[DD8%[DD
VUBO)6$3'&'3%S=('++'3%3#.()$*4%"#')%3#.()$*%')%)7660(*$-%',%
Q4P454
36!5b!
?',-0:)O!5b!8%
:',O!5b!8%!5b!
?',-0:)O)6$3'&'3%S=('++'3%3#.()$*4%"#')%3#.()$*%')%
)7660(*$-%',%Q4P454
36!5b5
?',-0:)O!5b58%
?',-0:)O)6$3'&'3%3#.()$*%&0(%?$)*$(,%_7(06$4
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Table 6.17. Supported Character Sets
Charset
Aliases
Description
!5b5
mUI[O`
A0'[O(78%A0'[(
`7))'.,4%"#')%3#.()$*%')%)7660(*$-%',%Q4P454
>Inb
cb\
"( '*'0,.+%S#',$)$8%<.',+=%7)$-%',%".':.,4
n>5P!5
cPD
B'<6+'&'$-%S#',$)$8%,.*'0,.+%)*., (-%3#.(.3*$(%)$*4
>InbO
GmBSB
%
>'/b%:'*#%G0,/%m0,/%$9*$,)'0,)8%"( '*'0,.+%S#',$)$4
B#'&*ehIB
BhIB8%cP5
h.6.,$)$4
_XSOhF
_XShF
h.6.,$)$4
6.3. Other String Functions
This chapter focused on some of the most useful string functions, but PHP provides other useful string functions, as
shown in Table 6.18.
Table 6.18. PHP Functions
Function
What+It+Does
addcslashes
l70*$)%)*(',/%:'*#%)+.)#$)%',%.%S%)*=+$4
addslashes
l70*$)%)*(',/%:'*#%)+.)#$)4
bin2hex
S0,1$(*)%2',.(=% *.%',*0%#$9 $3'<.+%($6($)$,*.*'0,4
chop
W+'.)%0&%rtrim()4
chr
`$*7(,)%.%)6$3'&'3%3#.(.3*$(4
chunk_split
B6+'*)%.%)*(',/%',*0%)<.++$(%3#7,A)4
convert_cyr_string
S0,1$(*)%&(0<%0,$%S=('++'3%3#.(.3*$(%)$*%*0%.,0*#$(4
convert_uudecode
V$30-$)%.%77$,30-$-%)*(',/4
convert_uuencode
X7$,30-$)%.%)*(',/4
count_chars
`$*7(,)%',&0(<.*'0,%.207*%3#.(.3*$()%7)$-%',%.%)*(',/4
crc32
S.+37+.*$)%*#$%3(3P5%60+=,0<'.+%0&%.%)*(',/4
crypt
U,$O:.=%)*(',/%$,3(=6*'0,%J#.)#',/K4
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Table 6.18. PHP Functions
Function
What+It+Does
echo
U7*67*)%0,$%0(%<0($%)*(',/)4
explode
B6+'*)%.%)*(',/%2=%)*(',/4
fprintf
?('*$)%.%&0(<.**$-%)*(',/%*0%.%)*($.<4
get_html_translation_table
`$*7(,)%*#$%*(.,)+.*'0,%*.2+$%7)$-%2=%htmlspecialchars()%
.,-%htmlentities()4
hebrev
S0,1$(*)%+0/'3.+%G$2($:%*$9*%*0%1')7.+% *$9*4
hebrevc
S0,1$(*)%+0/'3.+%G$2($:%*$9*%*0%1')7.+% *$9*%:'*#%,$:+',$%
30,1$()'0,4
html_entity_decode
S0,1$(*)%.++%G"]^%$,*'*'$)%*0%*#$'(%.66+'3.2+$%3#. (. 3*$()4
htmlentities
S0,1$(*)%.++%.66+'3.2+$%3#.(.3*$()%*0% G"]^%$,*'*'$)4
htmlspecialchars_decode
S0,1$(*)%)6$3'.+%G"]^%$,*'*'$)%2.3A%*0%3#.(. 3*$()4
htmlspecialchars
S0,1$(*)%)6$3'.+%3#.(.3*$()%*0%G"]^%$,*'*'$)4
implode
h0',)%.((.=%$+$<$,*)%:'*#%.%)*(',/4
join
W+'.)%0&%implode()4
levenshtein
S.+37+.*$)%^$1$,)#*$',%-')*.,3$%2$*:$$,%* :0%)*(',/)4
localeconv
n$*)%,7<$('3%&0(<.**',/%',&0(<.*'0,4
ltrim
B*('6)%:#'*$)6.3$%J0(%0*#$(%3#.(.3*$()K%&(0<%*#$%2$/',,',/%
0&%.%)*(',/4
md5_file
S.+37+.*$)%*#$%md5%#.)#%0&%.%/'1$,%&'+$4
md5
S.+37+.*$)%*#$%md5%#.)#%0&%.%)*(',/4
metaphone
S.+37+.*$)%*#$%<$*.6#0,$%A$=%0&%.%)*(',/4
money_format
C0(<.*)%.%,7<2$(%.)%.%37(($,3=%)*(',/4
nl_langinfo
l7$(=%+.,/7./$%.,-%+03.+$%',&0(<.*'0,4
nl2br
I,)$(*)%G"]^%+',$%2($.A)%2$&0($%.++%,$:+',$)%',%.%)*(',/4
number_format
C0(<.*)%.%,7<2$(%:'*#%/(076$-%*#07).,-)4
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Table 6.18. PHP Functions
Function
What+It+Does
ord
`$*7(,)%WBSII%1.+7$%0&%3#.(.3*$(4
parse_str
F.()$)%*#$%)*(',/%',*0%1.('.2+$)4
print
U7*67*)%.%)*(',/4
printf
U7*67*)%.%&0(<.**$-%)*(',/4
quoted_printable_decode
S0,1$(*)%.%;70*$-O6(',*.2+$%)*(',/%*0%.,%[O2'*%)*(',/4
quotemeta
l70*$)%<$*.3#.(.3*$()4
rtrim
B*('6)%:#'*$)6.3$%J0(%0*#$(%3#.(.3*$()K%&(0<%*#$%$,-%0&%.%
)*(',/4
setlocale
B$*)%+03.+$%',&0(<.*'0,4
sha1_file
S.+37+.*$)%*#$%sha1%#.)#%0&%.%&'+$4
sha1
S.+37+.*$)%*#$%sha1%#.)#%0&%.%)*(',/4
similar_text
S.+37+.*$)%*#$%)'<'+.('*=%2$*:$$,%*:0%)*(',/)4
soundex
S.+37+.*$)%*#$%)07,-$9%A$=%0&%.%)*(',/ 4
sprintf
`$*7(,)%.%&0(<.**$-%)*(',/4
sscanf
F.()$)%',67*%&(0<%.%)*(',/%.330(-',/%*0%.%&0(<.*4
str_ireplace
S.)$O',)$,)'*'1$%1$()'0,%0&%str_replace()4
str_pad
F )%.%)*(',/%*0%.%3$(*.',%+$,/*#%:'*#%.,0*#$(%)*(',/4
str_repeat
`$6$.*)%.%)*(',/4
str_replace
`$6+.3$)%.++%0337(($,3$)%0&%*#$%)$.(3#%)*(',/%:'*#%*#$%
($6+.3$<$,*%)*(',/4
str_rot13
F$(&0(<)%*#$%(0*!P%$,30-',/%0,%.%)*(',/4%B#'&*)%$1$(=%.+6#.%
3#.(.3*$(%!P%6+.3$)%',%*#$%.+6#.2$*4
str_shuffle
`.,-0<+=%)#7&&+$)%.%)*(',/4
str_split
S0,1$(*)%.%)*(',/%*0%.,%.((.=4
str_word_count
`$*7(,)%',&0(<.*'0,%.207*%:0(-)%7)$-%',%.%)*(',/4
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Table 6.18. PHP Functions
Function
What+It+Does
strcasecmp
>',.(=O).&$%3.)$O',)$,)'*'1$%)*(',/%30<6.(')0,4
strchr
W+'.)%0&%strstr()4
strcmp
>',.(=O).&$%)*(',/%30<6.(')0,4
strcoll
^03.+$O2.)$-%)*(',/%30<6.(')0,4
strcspn
C',-)%+$,/*#%0&%','*'.+%)$/<$,*%,0*%<.*3#',/%<.)A4
strip_tags
B*('6)%G"]^%.,-%FGF%*./)%&(0<%.%)*(',/4
stripcslashes
X,;70*$)%)*(',/%;70*$-%:'*#%addcslashes()4
stripos
C',-)%60)'*'0,%0&%&'()*%0337(($,3$%0&%.%3.)$O',)$,)'*'1$%)*(',/4
stripslashes
X,;70*$)%)*(',/%;70*$-%:'*#%addslashes()4
stristr
S.)$O',)$,)'*'1$%strstr()4
strlen
n$*)%)*(',/%+$,/*#4
strnatcasecmp
S.)$O',)$,)'*'1$%)*(',/%30<6.(')0,)%7)',/%.%L,.*7(.+%0(-$(M%
.+/0('*#<4
strnatcmp
B*(',/%30<6.(')0,)%7)',/%.%L,.*7(.+%0(-$(M%.+/0('*#<4
strncasecmp
>',.(=O).&$%3.)$O',)$,)'*'1$%)*(',/%30<6.(')0,%0&%*#$%&'()*%,%
3#.(.3*$()4
strncmp
>',.(=O).&$%)*(',/%30<6.(')0,%0&%*#$%&'()*%,%3#.(.3*$()4
strpbrk
B$.(3#$)%.%)*(',/%&0(%.,=%0&%.%)$*%0&%3#.(.3*$()4
strpos
C',-)%60)'*'0,%0&%&'()*%0337(($,3$%0&%.%)*(',/4
strrchr
C',-)%*#$%+.)*%0337(($,3$%0&%.%3#.(.3*$(%',%. % )*(',/4
strrev
`$1$()$)%.%)*(',/4
strripos
C',-)%60)'*'0,%0&%+.)*%0337(($,3$%0&%.%3.)$O',)$,)'*'1$%)*(',/%
',%.%)*(',/4
strrpos
C',-)%60)'*'0,%0&%+.)*%0337(($,3$%0&%.%3#.(.3*$(% ', %.%)*(',/4
strspn
C',-)%+$,/*#%0&%','*'.+%)$/<$,*%<.*3#',/%<.)A4
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Table 6.18. PHP Functions
Function
What+It+Does
strstr
C',-)%&'()*%0337(($,3$%0&%.%)*(',/4
strtok
"0A$,'a$)%)*(',/4
strtolower
].A$)%.%)*(',/%+0:$(3.)$4
strtoupper
].A$)%.%)*(',/%766$(3.)$4
strtr
"(.,)+.*$)%3$(*.',%3#.(.3*$()4
substr_compare
>',.(=O).&$%06*'0,.++=%3.)$O',)$,)'*'1$%30<6.(')0,%0&%*:0%
)*(',/)%&(0<%.,%0&&)$*8%76%*0%+$,/*#%3#.(.3*$()4
substr_count
S07,*)%*#$%,7<2$(%0&%)72)*(',/%0337(($,3$)4
substr_replace
`$6+.3$)%*$9*%:'*#',%.%60(*'0,%0&%.%)*(',/4
substr
`$*7(,)%6.(*%0&%.%)*(',/4
trim
B*('6)%:#'*$)6.3$%J0(%0*#$(%3#.(.3*$()K%&(0<%*#$%2$/',,',/%
.,-%$,-%0&%.%)*(',/4
ucfirst
].A$)%.%)*(',/H)%&'()*%3#.(.3*$(%766$(3.)$4
ucwords
X66$(3.)$)%*#$%&'()*%3#.(.3*$(%0&%$.3#%:0(-%',%.%)*(',/4
vfprintf
?('*$)%.%&0(<.**$-%)*(',/%*0%.%)*($.<4
vprintf
U7*67*)%.%&0(<.**$-%)*(',/4
vsprintf
`$*7(,)%.%&0(<.**$-%)*(',/4
wordwrap
?(.6)%.%)*(',/%*0%.%/'1$,%,7<2$(%0&%3#.(.3*$()%7)',/%.%)*(',/%
2($.A%3#.(.3*$(4
6.4. Chapter Summary
6.4.1. What You Should Know
Now that you have finished this chapter you should be able to answer the following questions:
1.
?#.*%')%.%)*(',/g
2.
?#$,%-0%=07%7)$%-072+$%;70*$)%0(%)',/+$%;70*$)g
3.
?#.*%.($%$)3.6$%3#.(.3*$()g
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
4.
V$&',$%:#'*$)6.3$4
5.
?#.*%&7,3*'0,)%+$*%=07%&0(<.*%)*(',/)g
6.
G0:%3.,%=07%/$*%*#$%+$,/*#%0&%.%)* (',/%.,-%307,*%*#$%3#.(.3*$()%',%.%)72 )*(',/g
7.
?#.*%&7,3*'0,)%30<6.($%)*(',/)g%W($%)*(',/)%3.)$%)$,)'*'1$%:#$,%*#$=%.($%
30<6.($-g
8.
?#.*%&7,3*'0,)%&',-%)'<'+.('*'$)%',%)*(',/)g
9.
G0:%-0%=07%&',-%.%60)'*'0,%',%.%)*(',/g %?#.*%&7,3*'0,)%+$*%=07%&',-%.%)6$3'&'$-%
60)'*'0,%',%.%)*(',/g
10.
?#.*%')%.%)72)*(',/g%?#.*%&7,3*'0,)%$9*(.3*%6.(*)%0&%.% )*(',/g
11.
?#.*%')%<$.,*%2=%L*('<<',/M%.,-%L6 ',/Mg
12.
?#.*%&7,3*'0,)%.($%7)$-%&0(%)$.(3#',/%.,-%($6+.3',/%:'*#',%)*(',/)g
13.
?#.*%.($%)6$3'.+%3#.(.3*$()g
14.
?#.*%.($%G"]^%$,*'*'$)g
15.
G0:%-0%=07%&0(3$%2($.A)%',%.%)*(',/g
16.
?#.*%&7,3*'0,)%-$.+%:'*#%#0<06#0,$)g
6.4.2. What’s Next?
Chapter 7, “Conditionals and Loops,” covers PHP conditional statements (if/else/elseif, switch) and loops
(while, for, foreach).
Chapter 6 Lab
1.
W))'/,%=07(%$O<.'+% ($))%*0%.%)*(',/4%X)$%FGF%)*(',/%&7,3*'0,)%*0%/$*%*#$%7 )$(%
,.<$%.,-%*#$%-0<.',%,.<$4
2.
S($.*$%*#$%&0++0:',/%)*(',/E%%
$string = " mary jones lives in santa cruz, california at 22 ocean
drive."
%
a. Find the number of characters in the string.
b. Capitalize all the letters in the string.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
c. Now make all the characters lowercase.
d. Use the substr() function to print Santa Cruz, California. The first letter in each
word will be in uppercase.
e. Use the substr() function to print Mary’s street address.
f. Trim out the whitespace at the beginning of the string.
g. Find the index position of California.
h. Replace Santa Cruz with Los Altos (case insensitive).
i. Find the number of words in the string.
3.
X)$%*#$%strcmp()%&7,3*'0,%*0%30<6.($%*#$%&0++0:',/%*:0%)*(',/)8%&'()*%30<6.(',/%
$str1%.,-%$str28%*#$,%30<6.(',/%$str2%.,-%$str14%_96+.',%*#$%1.+7$%*#.*%')%
($*7(,$-%&(0<%*#$%&7,3*'0,4%%
$str1="Pear"; $str2="Pearson";
%
What value is returned from strcmp() if you compare $str1 to $str1?
4.
?('*$%.%)3('6*%*0%/$,$(.*$%.%QO+$**$(%(.,-0<%6.)):0(-%:'*#%766$(3.)$8%+0:$(3.)$8%
.,-%,7<$('3%3#.(.3*$()4%B*0($%*#$%6.)):0(-%.)%.%1.('.2+$%.,-%-')6+.=%'*4B*.(*%:'*#E%%
$str =
"abcdefghijklmnopqrlsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
%
Hint: Try str_shuffle().
5.
S($.*$%.%)3.+.(%1.('.2+$%*0%30,*.',%.%<0,$=%.<07,*%0&%!bb\\\\\\4%X)$%*#$%
number_format()%&7,3*'0,%*0%-')6+.=%*#$%,7<2$(%.)%X4B4%-0++.()%:'*#%.%30<<.%
)$6.(.*0(%.,-%-$3'<.+%60',*4%Y0:%-')6+.=%*#$%).<$%,7<2$(%',%_7(0)%.)%'*%:07+-%
2$%&0(<.**$-%',%?$)*$(,%_7(06$%.,-%6+.3$%*#$%_7(0%)=<20+%2$&0($%*#$%,7<2$(%
Jop[PDQK4
%
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Chapter 7. Conditionals and Loops
7.1. Control Structures, Blocks, and Compound Statements
Figure 7.1 shows a flow chart. A flow chart is defined as a pictorial representation of how to plan the stages of a project.
It helps to visualize what decisions need to be made to accomplish a task. People control their lives by making
decisions, and so do programs. In fact, acccording to computer science books, a good language allows you to control
the flow of your program in three ways:
• Execute a sequence of statements.
• Based on a test, branch to an alternative sequence of statements.
• Repeat a sequence of statements until some condition is met.
Figure 7.1. A flow chart.
!
Well, then, PHP must be a good language.
We’ve already used programs that execute a sequence of statements, one after another. Now we examine the branching
and looping control structures that allow the flow of the program’s control to change depending on some conditional
expression.
The decision-making constructs (if, if/else, if/else if) contain a control expression that determines whether a
block of statements will be executed. The looping constructs (while, for) allow the program to repetitively execute a
statement block until some condition is satisfied.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
A compound statement or block consists of a statement or a group of statements surrounded by curly braces. The block
is syntactically equivalent to a single statement and usually follows an if, else, while, or for construct. This is a
block:
{ statement; statement; statement }
!
7.1.1. Conditionals
Conditional constructs control the flow of a program. If a condition is true, the program will execute a block of
statements and if the condition is false, flow will go to an alternate block of statements. Decision-making constructs
(if, else, switch) contain a control expression that determines whether or not a block of expressions will be
executed. If the condition after the if is met, the result is true, and the following block of statements is executed;
otherwise, the result is false and the block is not executed.
Format
if (condition){ statements; }
!
Example:
if ( $age > 21 ){ print "Let's Party!"; }
The block of statements (or single statement) is enclosed in curly braces. Normally statements are executed
sequentially. If there is only one statement after the conditional expression, the curly braces are optional.
if/else
“You better pay me now, or else . . .” Have you heard that kind of English statement before? PHP statements can be
handled the same way with the if/else branching construct. This construct allows for a two-way decision. The if
evaluates the first conditional expression in parentheses, and if the expression evaluates to true, the block after the
opening curly braces is executed; otherwise, the block after the else is executed. The else is optional. See Example
7.1.
Format
if (condition){ statements1; } else{ statements2; }
!
Example:
if ( $x > $y ){ print "$x is larger"; } else{ print "y is larger"; }
Example 7.1.
Code!View:!
!!!(The HTML Form)
<html><head><title>Your Fare</title></head>
<body bgcolor="lightgreen">
<font face="arial" size="+1">
<form method="get" action="iffare.php">
<p>How old are you?
<input type="text" name="age" size=2>
<p>
<input type="submit" name="submit_age" value="Get Fare" >
</form>
</body>
</html>
(The PHP Script)
<html><head><title>Your Fare</title></head>
<body bgcolor="lightgreen">
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Không có nhận xét nào:
Đăng nhận xét