Php Chapter 4 Training

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Favorites, Groups & Events

    Php Chapter 4 Training - Presentation Transcript

    1. Strings And Patterns Orlando PHP Meetup Zend Certification Training April 2009
    2. 3 Ways to Make Strings
      • Single Quote – Only ‘ needs to be escaped. No variable interpolation.
      • Double Quote – Variable interpolation and special escape tags.
        • $var = “This $string needs {$string}s. ”;
      • Heredoc (<<<) for multi-line strings
        • $string = <<<EOSTRING this is a $interpreted “string” on multiple $lines[10] EOSTRING
    3. String Functions
      • Strings as arrays: Don’t forget: zero-based!
        • $var = “abcdef”; $a = $var[0];
      • strlen($string) returns length in bytes
      • strtr($string, $find, $replace) - Transform
    4. Searching
      • Most string functions have case sensitive and case insensitive versions. (PHP5 added a lot of insensitive versions)
      • Most parameters can also be arrays of strings, operated on over the array.
      • strcmp($st1, $st2) returns 0 if equal
      • strpos($haystack, $needle) returns integer or FALSE if not found. Be sure to compare ===
      • strstr($haystack, $needle) returns rest of string, starting at needle, or FALSE
    5. Replacing
      • str_replace($find, $replace, $haystack)
        • Replaces all instances of $find with $replace
        • Really handy with $find as array and/or $replace as array
      • substr($string, $start, $length)
        • Returns a slice of the string
        • $start may be negative, for starting from end.
      • substr_replace($haystack, $replacement, $start, $length)
        • Only replaces a slice of the haystack
    6. Formatting
      • number_format($num, $decimals, $decimal_point, $thousands_sep)
      • Generic Formatting – sprintf()
        • Format String defines type (string, decimal), length, sign, special handling.
        • %d (decimal), %f (float), %s (string)
        • Modifiers: %4.3f shows 1234.56
        • sprintf(‘%4.3f is %d long’, 1234.563, 7) = “1234.56 is 7 long”
    7. Perl Regular Expressions
      • A pattern matching language that defines what to find when you don’t know exactly what you are looking for.
      • Match patterns
      Character class. Ex: [a-zA-z0-9s] [] Word (a-z or underline) w Digit. Equal to [0-9] d Whitespace (space, tab) S is non-whitespace s End of line $ Beginning of line ^ Any character except newline .
    8. Regular Expressions 2
      • Quantifiers
      • Patterns are wrapped in delimeters. Usually // but can be any character but backslash
      • Sub expressions
        • Parentheses around sub-matches
        • /abc(def[0-9])+ghi/ will match abcdef1ghi or abcdef1def2ghi
      Match the pattern between minimum and maximum times. {min,max} Zero or one time ? One or more times + Zero or more times *
    9. Regular Expression Functions
      • preg_match($pattern, $haystack, $matches)
        • Fills $matches array with strings from $pattern
        • Returns FALSE on no match
      • preg_replace($pattern, $replace, $haystack)
        • For each pattern, replace match(es) with $replace
        • If $replace is an array it will use each match to use one element from the array
    10. Summary
      • Strings are the most flexible of core data types. Can hold test or binary data.
        • Read the documentation to find string functions that do what you need.
        • Use “simple” string functions for search and replace when you can.
        • Be careful of PHP5 only functions, like the case insensitive versions.
        • Regular expressions are powerful and there are often more than one way to match a pattern.
    SlideShare Zeitgeist 2009

    + Chris ChubbChris Chubb Nominate

    custom

    138 views, 0 favs, 0 embeds more stats

    Training for the PHP Certification by Zend. Chapter more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 138
      • 138 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 7
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories