Download presentation
Presentation is loading. Please wait.
Published byJames Lawson Modified over 9 years ago
1
1 PHP Intro PHP Strings After this lecture, you should be able to: Manipulate and Output PHP Strings: Manipulate and Output PHP Strings: Single- or Double-quoted strings Single- or Double-quoted strings String Conversion String Conversion String Functions String Functions
2
2 PHP String PHP Strings A string can be single- or double-quoted. A string can be single- or double-quoted. An extensive set of functions for strings are provided: An extensive set of functions for strings are provided: Manipulation with a regular expression, Manipulation with a regular expression, Conversion between a string and an array, Conversion between a string and an array, Outputing by echo(), print(), printf(), and print_r(), and Outputing by echo(), print(), printf(), and print_r(), and Others Others
3
3 PHP String Single-Quoted String No evaluation occurs for a single-quoted string. $age = 20; echo 'I am $age.\n'; echo ''Single''; echo '\”Double\”'; echo '”Double”'; echo $age >= 18 ? 'OK' : ''; I am $age.\n (illegal) \“Double\” “Double” OK
4
4 PHP String Double-Quoted String $age = 20; echo “I am $age.\n”; echo “'Single'\n”; echo “\”Double\”\n”; echo “”Double””; echo $age >= 18 ? “OK” : “”; Variables and escape sequences within a double-quoted string are evaluated or replaced. I am 20. 'Single' “Double” (illegal) OK
5
5 PHP String int strlen(string) $str = 'Hello'; $len = strlen($str); echo $len; Returns the length of string string. 5
6
6 PHP String int strpos(string1, string2) $str = 'My dog house.'; $position = strpos($str, 'dog'); echo $position; Returns the numeric position of string2 in string1. 3
7
7 PHP String int strpos($str1, $str2 ): Warning Returns false if $str2 is not a substring of $str1. Returns false if $str2 is not a substring of $str1. Returns 0 if $str2 is at the beginning of $str1, so using if(strpos($str1, $str2) == 0) could cause an unreliable result. Returns 0 if $str2 is at the beginning of $str1, so using if(strpos($str1, $str2) == 0) could cause an unreliable result. if(strpos($str1, $str2) === 0) may be used. if(strpos($str1, $str2) === 0) may be used.
8
8 PHP String string substr (string, start[, length] ) Substring $str = “My dog.”; $str2 = substr($str, 3); echo $str2 $str3 = substr($str1, 3, 2); echo $str3 dog. do Returns the string present in string starting from location start until end of string.
9
9 PHP String array explode(delim, string): $str = “Kelley Engineering Center”; $arr = explode(" ", $str); echo “$arr[0]\n$arr[1]\n$arr[2]\n”; Creates an array of the substrings in string, separated by delim. Kelley Engineering Center
10
10 PHP String string implode(delim, array): $arr = array('S10', 'Wolf', '20'); $string = implode('|', $arr); echo $string Creates a string out of the elements in array, connected by delim. S10|Wolf|20
11
11 PHP String int ereg(pattern, string[, array]): Pattern Matching I ereg('boy', 'cowboys'); ereg('^boy', 'cowboys'); ereg('^cow', 'cowboys'); ereg('cow$', 'cowboys'); ereg('boys$', 'cowboys'); Tests whether or not string matches regular expression pattern. truefalsetruefalsetrue This function has been DEPRECATED as of PHP 5.3.0. preg_match() is an alternative.
12
12 PHP String Meta Characters for a Regular Expression ^ - start of line, or negation of characters characters $ - end of line. - any character [] – range, or set of characters () – grouping * - any number of times + - at least once ? - exactly once {n, m} – match n through m occurrences The following meta characters can be used in a regular expression. regular expression.
13
13 PHP String int ereg(pattern, string[, array]): Pattern Matching II ereg('.*', 'cowboys'); ereg('.......', 'cowboys'); ereg('c.*s', 'cowboys'); ereg('c.*s', 'cowgirls'); ereg('c.*s', 'tomboys'); ereg('^a*b*$', 'aaabb'); ereg('^a*b*$', 'bbbb'); ereg('^a*b*$', 'ccbbb'); ereg('^a+b*$', 'aabbb'); ereg('^a+b*$', 'bbb'); Wild card characters. truetruetruetruefalsetruetruefalsetruefalse
14
14 PHP String int ereg(pattern, string[, array]): Pattern Matching III ereg('^[a-z]*$', 'cowboy'); ereg('^[a-z]*$', 'cowboy123'); ereg('^[a-z]*[0-9]*$', 'cowboy123'); ereg('^[a-z]{6}$', 'cowboy'); ereg('^[a-z]{4}$', 'cowboy'); ereg('^[bcowy]*$', 'cowboy'); ereg('^[^0-9]$', 'a'); Specifying a range or set of valid characters. truefalsetruetruefalsetruetrue
15
15 PHP String int ereg(pattern, string[, array]): Extracting Substrings $email = “pham@eecs.oregonstate.edu”; ereg('(.*)@(.*)', $email, $arr); echo “User: $arr[1]\n”; echo “Host: $arr[2]\n”; echo “Email: $arr[0]\n”; User: pham Host: eecs.oregonstate.edu Email: pham@eecs.oregonstate.edu Retrieving text in groups.
16
16 PHP String Meta Characters for a Regular Expression ^ - start of line, or negation of characters characters $ - end of line. - any character [] – range, or set of characters () – grouping * - any number of times + - at least once ? - exactly once {n, m} – match n through m occurrences The following meta characters can be used in a regular expression. regular expression.
17
17 Example: is_safe_int() <?php function is_safe_int($var) { $pattern = "/^0$|^[1-9][0-9]*$/"; if(preg_match($pattern, $var)) { return true; } else { return false; } ?>
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.