Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 50 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
50
Dung lượng
1,81 MB
Nội dung
Explanation
!
"#$%G"]^%&0(<%')%)*.(*$-%#$($4%I*)%.3*'0,%3.++)%*#$%)3('6*%form.php4
5
"#$%FGF%)3('6*%')%)*.(*$-%#$($4%I*%:'++%6(03$))%*#$%&0(<%',67*4
P
I&%*#$%&0(<%#.)%2$$,%)72<'**$-8%*#$%1.+7$%$_POST['submit']%:'++%2$%)$*4
Q
"#$%$_POST%.((.=%30,*.',)%*#$%*$9*%*#.*%*#$%7)$(%*=6$-%',%*#$%*$9*%.($.%2098%
,.<$-%story4%JB$$%S#.6*$(%!\8%L]0($%0,%FGF%C0(<)8M%&0(%<0($%',&0(<.*'0,%0,%
*#$%$_POST%.((.=4K%C0(%,0:8%$input%30,*.',)%*#$%',67*%*=6$-%2=%*#$%7)$(4%B$$%
C'/7($%D4PD4
b
"#$%stripslashes()%&7,3*'0,%($<01$)%*#$%)+.)#$)%*#.*%:$($% $-%2=%
addslashes()8%*#$%-$&.7+*%2$#.1'0(%:#$,%*#$%magic_quotes_gpc%-'($3*'1$%#.)%
2$$,%*7(,$-%0,%',%*#$%php.ini%&'+$4
%
Figure 6.36. The user fills in a form.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Figure 6.37. After the form in Figure 6.36 is submitted. Output of Example 6.36.
%
%
%
6.2.15. Working with HTML Special Characters
Like all languages, HTML has certain characters that have special significance and those characters are represented by
HTML entities, special symbols starting with an ampersand and terminated with a semicolon; for example, the < and >
symbols are written as < and >. If the user enters text that contains these HTML characters (see Table 6.14)
when filling out a form, you can use the htmlspecialchars() function to convert the most common special
characters to their respective HTML entities. If you require all HTML character entities to be translated, use
htmlentities() shown in the next section.
Table 6.14. HTML Special Character Functions
Function
What+It+Does
htmlspecialchars_decode()
S0,1$(*)%)6$3'.+%G"]^%$,*'*'$)%2.3A%*0%3#.(. 3*$()
htmlspecialchars()
S0,1$(*)%)6$3'.+%3#.(.3*$()%*0%G"]^%$,*'*'$)
%
The htmlspecialchars() Function
The htmlspecialchars() function converts special characters (shown in Table 6.15) to HTML entities.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Table 6.15. Conversion of Special Characters
Character+Before
The+HTML+Entity
&%J.<6$().,-K
&
"%J-072+$%;70*$K
"%:#$,%ENT_NOQUOTES%')%,0*%)$*
'%J)',/+$%;70*$K
'%0,+=%:#$,%ENT_QUOTES%')%)$*
<%J+$))%*#.,K
<
>%J/($.*$(%*#.,K
>
%
Format
string htmlspecialchars (string string [, int quote_style [,
string charset]])
%
Example:
$text = "Isn't the << symbol is used in here-docs?"; echo
htmlspecialchars($text, ENT_QUOTES), "<br />";
You can define how to handle single or double quotes using the quote_style optional third argument, and define
the character set for your locale by using the optional fourth argument, called charset. Quote constants are shown in
Table 6.16.
Table 6.16. Quote Constants
Constant+Name
Description
ENT_COMPAT
S0,1$(*)%-072+$%;70*$)%.,-%+$.1$)%)',/+$%;70*$)%.+0,$8%*#$%-$&.7+*%<0-$
ENT_QUOTES
S0,1$(*)%20*#%-072+$%.,-%)',/+$%;70*$)
ENT_NOQUOTES
^$.1$)%20*#%-072+$%.,-%)',/+$%;70*$)%7, 30, 1$(*$-
%
Example 6.37 demonstrates how to use the htmlspecialchars() function. The output can be viewed in Figure
6.38.
Example 6.37.
<html><head><title>The htmlspecialchars() Function</title>
</head>
<body bgcolor="lavender">
<font size="+1">
<h3>Creating HTML Entities</h3>
<b>
<?php
1 $text = "<<Johnson&Son's Tobacco Store>>";
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
2 echo "Original: $text<br /><br />";
3 echo "Modified: ", htmlspecialchars($text), "<br />";
?>
</b>
</body>
</html>
Explanation
!
"#$%1.('.2+$8%$text8%')%.))'/,$-%.%)*(',/%30,*.',',/%)0<$%3#.(.3*$()%
30,)'-$($-%)6$3'.+%:#$,%($,-$($-%2=%*#$%G"]^%',*$(6($*$(@%*#$=%.($%*#$%<%.,-%
&%3#.(.3*$()4
5
?#$,%*#')%)*(',/%')%1'$:$-%',%*#$%2(0:)$(8%*#$%G"]^%',*$(6($*$(%)$$)%*#$%<%.)%
.,%06$,',/%*./%)=<20+%&0++0:$-%2=%.,%'(($+$1.,*%G"]^%$+$<$,*4%Y0*#',/%')%
-')6+.=$-%0*#$(%*#.,%*#$%,$9*%06$,',/%<%.,-%*#$%3+0)',/%>4
P
"#$%htmlspecialchars()%&7,3*'0,%3($.*$)%G"]^%$,*'*'$)%07*%0&%*#$%<%.,-%&%)0%
*#.*%*#$%2(0:)$(%3.,%-')6+.=%*$9*4%Y0*$%*#.*%*#$%)07(3$%6./$%J)$$%C'/7($%D4PcK%
&0(%*#$%?$2%6./$%)#0:)%*#.*%*#$)$%$,*'*'*'$)% . ($%,0:%6.(*%0&%*#$%6./$4
Figure 6.38. Converting special characters to HTML entities. Output from Example 6.37.
%
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
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.
[...]... is meant by “trimming” and “padding”? 12 What functions are used for searching and replacing within strings? 13 What are special characters? 14 What are HTML entities? 15 How do you force breaks in a string? 16 What functions deal with homophones? 6.4.2 What’s Next? Chapter 7, “Conditionals and Loops,” covers PHP conditional statements (if/else/elseif, switch) and loops... dollars with a comma separator and decimal point Now display the same number in Euros as it would be formatted in Western Europe and place the Euro symbol before the number (€) 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... name, submit_fare, that will be used in the following PHP script 4 The opening PHP tag tells PHP to start processing 5 Now we are looking at the PHP instructions If the user pressed the submit button, this file will execute PHP will store the names of the input devices in variables, $age and $submit_fare This conditional expression tests to... Figure 7.11 The HTML form from Example 7.7 with checked boxes Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark Figure 7.12 Output from the PHP program Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 7.2.4 The foreach Loop The foreach loop is designed to work with arrays and works only with arrays Arrays are covered in Chapter 8, “Arrays,” but... Nested Loops A loop within a loop is a nested loop A common use for nested loops is to display data in rows and columns where one loop handles the rows and the other handles the columns The outside loop is initialized and tested; the inside loop then iterates completely through all of its cycles; and the outside loop starts again where it left off The inside loop moves faster than the outside loop Loops... York, London, Paris, Honolulu, and Tokyo Write a PHP script that uses a switch statement to evaluate each of the cities, and based on the city selected by the user, sends back a message such as, “Welcome to San Francisco Be sure to bring your heart and a jacket!” or “Bonjour, let’s take a stroll on the left bank!” Create the following form that uses radio buttons: Create a PHP script that will use case... Explanation 1 The PHP script starts here 2 If $submit_color has not been set, then the form was not submitted 3 The PHP program starts here 4 In the switch expression, the value of $color is matched against the values of each of the following case labels (PHP names the variable, $color, by the same name assigned to the radio button and assigns to... variable, then "green" is tested, and there is a match, its block of statements is executed purchase PDF Split-Mergeifon www.verypdf.com to remove this watermark The break statement sends control of the program to line 9 blue and the message “Font is blue” will appear in the table cell The break statement sends control of the program to line 9 If "red" and "blue" are not matched... end of the for loop Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark Figure 7.10 The for loop The for Loop and Repetitive Form Fields The HTML form in Example 7.7 consists of a set of five check boxes with repetitive names The only part of the name that differs is the number value fixed to the end of the name By using a for loop and variable variables[1], you can dynamically... -(The PHP Script) Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark Checkbox Item Checked Values 1 2 3 4 . have special significance and those characters are represented by
HTML entities, special symbols starting with an ampersand and terminated with a semicolon;.
%
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