170 likes | 307 Views
PHP Strings. After this lecture, you should be able to: Manipulate and Output PHP Strings : Single- or Double-quoted strings String Conversion String Functions. PHP Intro. PHP Strings. A string can be single- or double-quoted .
E N D
PHP Strings After this lecture, you should be able to: Manipulate and Output PHP Strings: Single- or Double-quoted strings String Conversion String Functions PHP Intro
PHP Strings • A string can be single- or double-quoted. • An extensive set of functions for strings are provided: • Manipulation with a regular expression, • Conversion between a string and an array, • Outputing by echo(), print(), printf(), and print_r(), and • Others
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
Double-Quoted String Variables and escape sequences within a double-quoted string are evaluated or replaced. $age = 20; echo “I am $age.\n”; echo “'Single'\n”; echo “\”Double\”\n”; echo “”Double””; echo $age >= 18 ? “OK” : “”; I am 20. 'Single' “Double” (illegal) OK
int strlen(string) Returns the length of stringstring. • $str = 'Hello'; • $len = strlen($str); • echo $len; 5
int strpos(string1, string2) Returns the numeric position of string2 in string1. • $str = 'My dog house.'; • $position = strpos($str, 'dog'); • echo $position; 3
int strpos($str1, $str2 ): Warning • 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. • if(strpos($str1, $str2) === 0) may be used.
string substr(string, start[, length])Substring $str = “My dog.”; $str2 = substr($str, 3); echo $str2 $str3 = substr($str1, 3, 2); echo $str3 Returns the string present in string starting from location start until end of string. dog. do
array explode(delim, string): Creates an array of the substrings in string, separated by delim. $str = “Kelley Engineering Center”; $arr = explode(" ", $str); echo “$arr[0]\n$arr[1]\n$arr[2]\n”; Kelley Engineering Center
string implode(delim, array): Creates a string out of the elements in array, connected by delim. $arr = array('S10', 'Wolf', '20'); $string = implode('|', $arr); echo $string S10|Wolf|20
int ereg(pattern, string[, array]): Pattern Matching I Tests whether or not string matches regular expression pattern. ereg('boy', 'cowboys'); ereg('^boy', 'cowboys'); ereg('^cow', 'cowboys'); ereg('cow$', 'cowboys'); ereg('boys$', 'cowboys'); true false true false true This function has been DEPRECATED as of PHP 5.3.0. preg_match() is an alternative.
Meta Characters for a Regular Expression ^ - start of line, or negation of 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. PHP String
int ereg(pattern, string[, array]): Pattern Matching II Wild card characters. 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'); true true true true false true true false true false
int ereg(pattern, string[, array]): Pattern Matching III Specifying a range or set of valid characters. 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'); true false true true false true true
int ereg(pattern, string[, array]): Extracting Substrings Retrieving text in groups. $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
Meta Characters for a Regular Expression The following meta characters can be used in a regular expression. ^ - start of line, or negation of 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
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; } } ?>