mcis ua
play

MCIS/UA PHP Training 2003 Chapter 6 Strings String Literals - PowerPoint PPT Presentation

MCIS/UA PHP Training 2003 Chapter 6 Strings String Literals Single-quoted strings Double-quoted strings escape sequences String Literals Interpreted items Single-quoted Double-quoted \' - single quote variables - value of


  1. MCIS/UA PHP Training 2003 Chapter 6 Strings

  2. String Literals • Single-quoted strings • Double-quoted strings escape sequences

  3. String Literals Interpreted items Single-quoted Double-quoted \' - single quote variables - value of variable \\ - backslash \n - new line \r - return \t - tab \\ - backslash \$ - dollar sign \" - double quote \ nnn - ASCII char in octal \x nn - ASCII char in hex heredoc

  4. Here Documents • The <<< operator (also known as heredoc) can be used to construct multi-line strings. $myString = <<< End This is a multi-line string. This is the second line. This is the third line. End; print $myString; This is a multi-line string. This is the second line. This is the third line. →

  5. Here Documents $name = $info->queryFirstColumn( "SELECT name ". "FROM info ". "WHERE uniqueid = ?", $uid); $sql = <<< end_of_sql SELECT name FROM info WHERE uniqueid = ? end_of_sql; $name = $info->queryFirstColumn($sql, $uid); →

  6. Here Documents $name = $info->queryFirstColumn(<<< end_of_sql SELECT name FROM info WHERE uniqueid = ? end_of_sql , $uid); outputting

  7. Outputting strings • Several ways to output strings and other data: echo print printf sprintf print_r var_dump echo and print

  8. echo and print • echo and print output data and are pretty much identical $x = "abc"; echo "value: $x"; → value: abc print "value: $x"; → value: abc printf

  9. printf • printf outputs formatted data $x = 5; printf("value: %d", $x); → value: 5 $mon = 4; $day = 8; $year = 2003; printf("%d/%d/%d", $mon, $day, $year); 4/8/2003 format codes

  10. printf Format Codes format result input output %% percent sign % %d integer -123.45 -123 %f floating point -123.45 -123.450000 %s string abc abc %b binary 5 101 %c character 65 A %o octal 255 377 %x hex (lowercase) 123456789 75bcd15 %X hex (uppercase) 123456789 75BCD15 %u unsigned integer -123.45 4294967173 modifiers

  11. printf • Additional modifiers may be added between the % and the letter (in the following order) • 0 - do zero padding rather than space padding • minus sign - forces left-justification • number - minumum number of characters to output; for floats - number of digits before the decimal point • .number - for floats, the number of decimal digits modifier examples

  12. printf printf("%5d", 3); ____3 printf("%-5d", 3); 3____ printf("%05d", 3); 00003 printf("%8f", 3.5); _______3.500000 printf("%8.3f", 3.5); _______3.500 printf("%08.3f", 3.5); 00000003.500 →

  13. printf $mon = 4; $day = 8; $year = 2003; printf("%d/%d/%d", $mon, $day, $year); 4/8/2003 printf("%02d/%02d/%04d", $mon, $day, $year); 04/08/2003 sprintf

  14. sprintf • sprintf formatted data and returns a string $x = sprintf("%05d", 3); print $x; 00003 print_r

  15. print_r • print_r can be used to dump variables $x = array(1, "abc", array("covertka" => "Kent", "kingmatm" => "Tim")); print_r($x); Array ( [0] => 1 [1] => abc [2] => Array ( [covertka] => Kent [kingmatm] => Tim ) ) var_dump

  16. var_dump • var_dump can also be used to dump variables $x = array(1, "abc", array("covertka" => "Kent", "kingmatm" => "Tim")); var_dump($x); array(3) { [0]=> int(1) [1]=> string(3) "abc" [2]=> array(2) { ["covertka"]=> string(4) "Kent" ["kingmatm"]=> string(3) "Tim" } string functions }

  17. Useful string functions • There are many functions that can be used to manipulate strings: strlen() - string length trim(), ltrim(), rtrim() - trim strings strtoupper(), strtolower() - change case ucfirst(), ucwords() - uppercases the first character or first character of all words of a string →

  18. Useful string functions substr() - returns part of a string strpos() - finds a substring within string explode() - splits a string into parts based on a seperator - returns an array implode() - concatenates all of the elements of an array together separated by a character →

  19. Useful string functions str_repeat() - used to repeat a string a number of times str_pad - pads a string with another string to a particular length (left, right, or both) strcasecmp() - compares 2 strings ignoring case parse_url() - parses a URL into it's parts (scheme, host, port, path, etc.) →

  20. Useful string functions htmlentities(), htmlspecialchars() - encodes html special characters (< > & ' ", etc.) rawurlencode(), rawurldecode(), urlencode(), urldecode() - encodes a URL regular expressions

  21. Regular Expressions 555-1212 preg_match('/\d\d\d-\d\d\d\d/', $phone); 513-555-1212 preg_match('/(\d\d\d-)?\d\d\d-\d\d\d\d/', $phone); (513)555-1212 or (513) 555-1212 preg_match('/\(?(\d\d\d-)?\)?\s?\d\d\d-\d\d\d\d/', $phone); parse the parts into variables preg_match('/\(?(\d\d\d)?-?\)?\s?(\d\d\d)-(\d\d\d\d)/', $phone, $parts); →

  22. Regular Expressions • The regular expression functions are used to perform various pattern matching activities: preg_match(), preg_match_all() - match (and extract) a pattern or all patterns from a string. preg_replace() - replace substrings that match a pattern with another substring. preg_split() - split a string based on a pattern. preg_grep() - find all elements of an array that match a pattern. →

  23. Regular Expressions $x = 'abcdefghijk'; if (preg_match('/def/', $x)) { print "Found the string."; } patterns

  24. Patterns • Patterns are enclosed within delimiters - usually slashes /def/ →

  25. Patterns • Some characters have special meanings Matches Pattern True False . any single characters /c.t/ cat, execute coat ^ beginning of string /^cat/ cat, caterpiller application $ end of string /cat$/ cat, wildcat application | or /cat|dog/ application, dogged \b word boundary /\bcat/ cat, catalog, big cat wildcat \B non-word boundary /\Bcat/ wildcat cat, catalog →

  26. Patterns Matches Pattern True False \s whitespace /c\st/ mac technology cat \S non-whitespace /c\St/ application mac tech \w word character (0-9,a- /c\wt/ cat, application c$t z,A-Z,_) \W non-word character /c\Wt/ c$t cat \d digit /\d\d\d/ 123, abc123def a1b2c3 \D non-digit /\D\D\D/ some text 123ab456 modifiers

  27. Patterns • A backslash can be used to "escape" any reserved characters /c.t/ - matches c.t and also cat, cot, cut /c\.t/ - matches c.t and nothing else →

  28. Patterns • Custom character patterns can be constructed using square brackets /c[aou]t/ - matches cat, cot, or cut, but not cet or cit • a hyphen can be used to specify a range /c[a-fu-z]t/ - matches cat, cbt, cct, or cut, cvt, czt but not cit or cot /[0-9a-fA-F]/ - matches a hexadecimal character →

  29. Patterns • a caret after the [ indicates negation /c[^aou]t/ - matches cet, cit, but not cat, cot, or cut /c[aou^ei]t/ - can't mix - caret must be at the beginning - if not, it's treated like a caret quantity modifiers

  30. Pattern Quantity Modifiers Matches Pattern True False * 0 or more times /ca*t/ ct, cat, caaaat cabat + 1 or more times /ca+t/ cat, caaaat ct ? 0 or 1 times /ca?t/ ct, cat caaaat ct, cat, {n} Exactly n times /ca{3}t/ caaat caaaat Between n and m {n,m} /ca{1,3}t/ cat, caat, caaat ct, caaaat times (inclusively) {n,} At least n times /ca{2,}t/ caat, caaaaat ct, cat parens

  31. Patterns • Parenthesis have 2 purposes: • Grouping • Remembering →

  32. Patterns • Parenthesis for grouping 555-1212 or 513-555-1212 /\d?\d?\d?-?\d\d\d-\d\d\d\d/ /\d{3}?-?\d\d\d-\d\d\d\d/ /(\d\d\d-)?\d\d\d-\d\d\d\d/ /(\d{3}-)?\d{3}-\d{4}/ →

  33. Patterns • Parenthesis for remembering $phone = "The phone number is 555-1212."; preg_match('/(\d{3}-)?(\d{3})-(\d{4})/', $phone, $parts); print_r($parts); Array ( [0] => 555-1212 [1] => [2] => 555 [3] => 1212 ) →

  34. Patterns $phone = "The phone number is 513-555-1212."; preg_match('/(\d{3}-)?(\d{3})-(\d{4})/', $phone, $parts); print_r($parts); Array ( [0] => 513-555-1212 [1] => 513- [2] => 555 [3] => 1212 ) →

  35. Patterns /(\d{3})?-?(\d{3})-(\d{4})/ - allows for -555-1212 or 513555-1212 /((\d{3})-)?(\d{3})-(\d{4})/ Array ( [0] => 513-555-1212 [1] => 513- [2] => 513 [3] => 555 [4] => 1212 ) →

  36. Patterns • A ?: following an open paren causes the paren to be used for grouping but not remembering /(?:(\d{3})-)?(\d{3})-(\d{4})/ Array ( [0] => 513-555-1212 [1] => 513 [2] => 555 [3] => 1212 ) →

  37. Patterns • Rememberd patterns can also be referenced using \1, \2, \3, etc. /(.)(.)\2\1/ Would match - abba, toot, otto, dood Would not match - abab abaa, abbb trailing options

  38. Trailing options • Various modifiers can be added after the trailing slash • i - ignore case /[0-9a-fA-F]/ - hexadecimal character /[0-9a-f]/i - same functions

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend