Title: HINF 6220 Networks and the Web for Health Informatics
1Tutorial 11
HINF 6220 (Networks and the Web for Health
Informatics)
PHP
kharrazi_at_cs.dal.ca http//flame.cs.dal.ca/kharraz
i/
2Review Tutorial 10
- PHP Intro
- PHP Syntax lt?php ?gt
- PHP echo echo 'something'
- PHP Commenting // or / /
- PHP Variables x string, number, array
3Have you downloaded theTutorialApps
from http//www.cs.dal.ca/kharrazi/teaching/hin
f_6220/TutorialApps.zip ?
4TutorialApps.zip
5PHP in a Nutshell
- PHP String Manipulations
- PHP Array Manipulations
- PHP Conditions
- PHP Loops
- PHP Functions
- PHP Cookies
- PHP SSI
- PHP Forms
- PHP/MySQL Integration
61. PHP String Manipulation
- These functions all manipulate strings in various
ways. Some more specialized sections can be done
by regular expressions (like Perl) and URL
handling commands. - No external libraries are needed to build this
extension. - There is no installation needed to use these
functions they are part of the PHP core. - For regular expressions please refer to
http//ca.php.net/manual/en/ref.regex.phphttp//c
a.php.net/manual/en/ref.pcre.php
7PHP String Manipulation (ctn.)
- Formatting Strings
- Trimming, Case Changing
- Joining and Splitting Strings
- Comparing Strings
- Match and Replace Substrings
- Regular expression
- please refer to
- http//ca.php.net/manual/en/ref.regex.phphttp//
ca.php.net/manual/en/ref.pcre.php
8PHP StringFormatting (ctn.)
- Trimming Stripping white space (or other
characters) from the beginning and/or end of a
string. - Usually we use these functions to remove
additional spaces or characters that users might
have typed mistakenly in the input forms. - syntax means that is optional.
trim (string, character list) ltrim (string,
character list) rtrim (string, character
list) chop (string)
9PHP StringFormatting (ctn.)
trim
lt?php text " . ... .email me your email... .
. " // Trim echo "Text "."".text."" ec
ho "ltbrgt" echo "Trim "."".trim(text)."" e
cho "ltbrgt" echo "Trim . and space
"."".trim(text, ". ")."" echo "ltbrgt" echo
"Trim e,m,a,i,l,. and space "."".trim(text,
"email. ")."" ?gt
10PHP StringFormatting (ctn.)
trim
TRIM removes the indicated characters (in
this case e, m, a, i, l, . and space from both
sides of the string variable. TRIM does not
change the variable itself and therefore the
variable will remain intact.
11PHP StringFormatting (ctn.)
ltrim
lt?php text " . ... .email me your email... .
. " // Left Trim echo "Text
"."".text."" echo "ltbrgt" echo "Trim
"."".ltrim(text)."" echo "ltbrgt" echo "Trim
. and space "."".ltrim(text, ". ")."" echo
"ltbrgt" echo "Trim e,m,a,i,l,. and space
"."".ltrim(text, "email. ")."" ?gt
12PHP StringFormatting (ctn.)
ltrim
LTRIM removes the indicated characters (in this
case e, m, a, i, l, . and space from the left
side (beginning) of the string variable.LTRIM
does not change the variable itself and therefore
the variable will remain intact.
13PHP StringFormatting (ctn.)
rtrim
lt?php text " . ... .email me your email... .
. " // Right Trim echo "Text
"."".text."" echo "ltbrgt" echo "Trim
"."".rtrim(text)."" echo "ltbrgt" echo "Trim
. and space "."".rtrim(text, ". ")."" echo
"ltbrgt" echo "Trim e,m,a,i,l,. and space
"."".rtrim(text, "email. ")."" ?gt
14PHP StringFormatting (ctn.)
rtrim
RTRIM removes the indicated characters (in this
case e, m, a, i, l, . and space from the right
side (end) of the string variable.RTRIM does not
change the variable itself and therefore the
variable will remain intact.
15PHP StringFormatting (ctn.)
chop
lt?php text " . ... .email me your email... .
. " // Chop echo "Text "."".text."" ec
ho "ltbrgt" echo "Trim "."".chop(text)."" e
cho "ltbrgt" echo "Trim . and space
"."".chop(text, ". ")."" echo "ltbrgt" echo
"Trim e,m,a,i,l,. and space "."".chop(text,
"email. ")."" ?gt
16PHP StringFormatting (ctn.)
chop
Chop behaves exactly like RTRIM. Chop does not
change the variable itself and therefore the
variable will remain intact.
17PHP StringFormatting (ctn.)
- Case Changing These functions will change the
case of a particular character to lower or upper. - Usually we use these functions to format the
users input data received by forms. - Remember that UNIX systems are case sensitive!
strtoupper (string) strtolower (string) ucfirst
(string) ucwords (string)
18PHP StringFormatting (ctn.)
strtoupper
strtolower
lt?php text "Hello World!" echo "Text
".text // strtoupper changes all of the
characters to upper case echo "ltbrgt" echo
"strtoupper ".strtoupper(text) //
strtoupper changes all of the characters to upper
case echo "ltbrgt" echo "strtolower
".strtolower(text) ?gt
19PHP StringFormatting (ctn.)
strtoupper
strtolower
STRTOUPPER makes a string upper case. STRTOLOWER
makes a string lower case.
20PHP StringFormatting (ctn.)
ucfirst
ucwords
lt?php text "this is my sentence!" echo
"Text ".text // ucfirst make a string's
first character uppercase echo "ltbrgt" echo
"ucfirst ".ucfirst(text) // ucwords
Uppercase the first character of each word in a
string echo "ltbrgt" echo "ucwords
".ucwords(text) ?gt
21PHP StringFormatting (ctn.)
ucfirst
ucwords
UCFIRST make a string's first character
uppercase UCWORDS Uppercase the first character
of each word in a string
22PHP StringFormatting (ctn.)
- These commands are recommended for reading
- print()
- printf()
- sprintf()
- print_r()
- nl2br()
- addslashes()
- stripslashes()
- Please refer to the following link for more
information - http//ca.php.net/manual/en/ref.strings.php
-
23PHP StringJoinSplit
- Joining These functions will join elements of an
array to make a string. - Splitting These functions will split a string
and create an array from the fragments.
implode (seperator, array elements) join
(seperator, array elements)
explode (seperator, string, limit) split
(regular expression, string) substr (string,
start position, length)
24PHP StringJoinSplit (ctn.)
implode
lt?php text array ("Peter", "Mike", "Brian",
"Robin") print_r(text) // implode joins
array elements with a string echo "ltbrgt" echo
"implode " . implode(",", text) ?gt
In this case , (comma) is the delimiter or the
glue to join the array elements.
25PHP StringJoinSplit (ctn.)
implode
IMPLODE joins array elements with a string. JOIN
works the same as IMPLODE.
26PHP StringJoinSplit (ctn.)
explode
lt?php text "This is my sentence" //
explode splits a string by the string delimiter
in an array echo "ltbrgt" echo "explode " .
explode(" ", text) echo "ltbrgt" x
explode(" ", text) print_r(x) ?gt
In this case (space) is the delimiter or the
splitter to split the string into fragments of
then putting them as array elements.
27PHP StringJoinSplit (ctn.)
explode
EXPLODE splits a string by the string delimiter
in an array
28PHP StringJoinSplit (ctn.)
substr
lt?php text "This is my sentence" echo
"ltpregt" echo "Text " . text echo
"ltbrgt" echo "......0123456789012345678" echo
"lt/pregt" // substr returns part of a
string echo "ltbrgt" echo "substr start 1 " .
substr(text, 1) echo "ltbrgt" echo "substr
start 5 " . substr(text, 5) echo "ltbrgt"
echo "substr start 0 length 4 " . substr(text,
0, 4) echo "ltbrgt" echo "substr start 2 length
4 " . substr(text, 2, 4) echo "ltbrgt" echo
"substr start -1 length 1 " . substr(text, -1,
1) ?gt
substr (string, num1, num2) The first number is
the starting position in the string and the
second number is the length of the substring.
29PHP StringJoinSplit (ctn.)
substr
SUBSTR returns part of a string
30PHP StringComparision
- strcmp() will compare two given strings and
returns lt 0 if str1 is less than str2 gt 0 if
str1 is greater than str2, and 0 if they are
equal. - strcasecmp() is the non-case sensitive version of
it. - strlen() returns the length of a string
strcmp (string_1, string_2) strcasecmp
(string_1, string_2)
strlen (string)
31PHP StringComparision (ctn.)
strcmp
strcasecmp
lt?php text1 "ABC" text2 "abc" echo
"Text1 " . text1 echo "ltbrgt" echo "Text2
" . text2 echo "ltbrgt" // strcmp is a
binary safe comparision echo "ltbrgt" echo
"strcmp " . strcmp(text1, text2) //
strcasecmp is a binary safe case insensitive
comparision echo "ltbrgt" echo "strcasecmp "
. strcasecmp(text1, text1) ?gt
32PHP StringComparision (ctn.)
strcmp
strcasecmp
STRCMP resulted in -32 because the ASCI code of
A is 32 numbers below a
33PHP StringComparision (ctn.)
strlen
lt?php text "This is my sentence" echo
"ltpregt" echo "Text " . text echo
"ltbrgt" echo "......0123456789012345678" echo
"lt/pregt" // strlen gets the string's
length echo "ltbrgt" echo "strlen " .
strlen(text) ?gt
34PHP StringComparision (ctn.)
strlen
STRLEN shows the length of a string
35PHP StringMatchReplace
- Match The commands match a specific substring in
a longer string. - Replace The commands replace a substring of a
longer string.
strstr (string, substring) strchr (string,
substring) strrchr (string, substring) stristr
(string, substring)
str_replace (sub_string, sub_new, string)
36PHP StringMatchReplace (ctn.)
strstr
lt?php email "yourname_at_yoursite.com" echo
"Email " . email echo "ltbrgt" // strstr
finds the first occurrence of a string echo
"ltbrgt" echo "strstr for _at_ " . strstr(email,
"_at_") echo "ltbrgt" echo "strstr for . " .
strstr(email, ".") echo "ltbrgt" echo "strstr
for r " . strstr(email, "r") ?gt
In these case _at_, . and r have been used as the
determining characters.
37PHP StringMatchReplace (ctn.)
strstr
STRSTR finds the first occurrence of a
string STRCHR is the same as STRSTR STRISTR is
the case insensitive of STRSTR
38PHP StringMatchReplace (ctn.)
strrchr
lt?php email "yourname_at_yoursite.com" echo
"Email " . email echo "ltbrgt" // strrchr
finds the last occurrence of a character in a
string echo "ltbrgt" echo "strrchr for _at_ " .
strrchr(email, "_at_") echo "ltbrgt" echo
"strrchr for . " . strrchr(email, ".") echo
"ltbrgt" echo "strrchr for r " .
strrchr(email, "r") ?gt
In these case _at_, . and r have been used as the
determining characters.
39PHP StringMatchReplace (ctn.)
strrchr
STRRCHR finds the last occurrence of a character
in a string, therefore for the last case it did
not find rname_at_yoursite.com as it always looks
for the last occurance.
40PHP StringMatchReplace (ctn.)
str_replace
lt?php text "Peter is a teacher. Peter goes to
the school everyday." echo "Text " . text
echo "ltbrgt" // str_replace replaces all
occurrences of the // search string with the
replacement string echo "ltbrgt" echo
"str_replace replacing Peter with Mark " .
str_replace("Peter", "Mark", text) ?gt
In these case Peter has been replaced with
Mark anywhere in the string.
41PHP StringMatchReplace (ctn.)
str_replace
STR_REPLACE replaces all occurrences of the
search string with the replacement string
42Summary
- PHP String Manipulations
- Formatting Strings
- Joining and Splitting Strings
- Comparing Strings
- Match and Replace Substrings
43Next Sessions
- PHP Array Manipulations
- PHP Conditions
- PHP Loops
- PHP Functions
- PHP Cookies
- PHP SSI
- PHP Forms
- PHP/MySQL Integration
44Exercise
- Please refer to the available text file in the
slides section for session 11 on the tutorials
website - http//flame.cs.dal.ca/kharrazi/teaching/hinf_62
20/slides.php
45Tutorial References
- Readings
- PHP Reference
- http//ca.php.net/docs.php
- Online HTML Resources
- http//www.w3schools.com/html/default.asp
- Online PHP Resources
- http//www.w3schools.com/php/default.asp