1 / 17

PHP Strings

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 .

alpha
Download Presentation

PHP Strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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. 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

  5. int strlen(string) Returns the length of stringstring. • $str = 'Hello'; • $len = strlen($str); • echo $len; 5

  6. int strpos(string1, string2) Returns the numeric position of string2 in string1. • $str = 'My dog house.'; • $position = strpos($str, 'dog'); • echo $position; 3

  7. 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.

  8. 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

  9. 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

  10. 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

  11. 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.

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  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; } } ?>

More Related