1 / 13

PHP Using Strings

PHP Using Strings. Matching & replacing with string functions. Replacing substrings (replace certain parts of a document template; ex <name> with client’s name etc) mixed str_replace (mixed $needle , mixed $ new_needle , mixed $haystack [, int $count ] )

krystac
Download Presentation

PHP Using 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 Using Strings

  2. Matching & replacing with string functions Replacing substrings (replace certain parts of a document template; ex <name> with client’s name etc) • mixed str_replace (mixed $needle, mixed $new_needle, mixed $haystack[, int $count] ) • Replaces all instances of $needle in $haystack with $new_needle • Returns a new string = a copy of $haystack with all replacements made • $count has to be a variable (lhs) that will be set to the number of replacements made • $needle, $new_needle, $haystack can be strings or arrays

  3. Feeback.html • http://www.nku.edu/~frank/csc301/Examples/PHP_Strings/feedback.html • http://www.nku.edu/~frank/csc301/Examples/PHP_Strings/processfeedback_v2_php.pdf

  4. Regular Expressions • The match function is • int preg_match(string $pattern, string $str [, array &$matches [, …]]) • 1st argument is the pattern • 2nd argument is the subject string • Returns number of matches found: • 1(true) if there is a match – it stops at the first match found! • 0(false) otherwise • An optional 3rd argument will capture the actual matched stringand the parenthesized subpatterns, if any. • int preg_match_all(…) • Use to find all matches to the regular expression

  5. Character Classes • [A-Z] uppercase letter • [a-z] lowercase letter • [0-9] digit • {5} exactly five • {3,} three or more • {1,4} one to four

  6. Metacharacters • ^ beginning of string • $ end of string

  7. Regular Expression • Username – 5 to 10 lowercase letters? $pattern = '/^[a-z]{5,10}$/'; if (!preg_match($pattern, $username)) { echo "<p>$username is invalid.</p>"; }

  8. Regular Expressions • Social Security Number 123-45-6789 $pattern = '/^[0-9]{3}-[0-9]{2}-[0-9]{4}$/'; if (!preg_match($pattern, $ssn)) { echo "<p>$ssn is invalid.</p>"; }

  9. Character classes • \d any digit • \D any non-digit • \w Any letter, number, or underscore • \W Anything that is not a letter, number, or underscore • \s whitespace (space, tab, new line, etc.) • \S An y non-whitespace

  10. Regular Expressions • Social Security Number 123-45-6789 $pattern = '/^\d{3}-\d{2}-\d{4}$/'; if (!preg_match($pattern, $ssn)) { echo "<p>$ssn is invalid.</p>"; }

  11. Metacharacters • [0-9]* * = zero or more • [A-Z]+ + = one or more • . . = match any character • ([A-Z]\. )? ? = either zero or one

  12. Regular Expressions • Last name: Frank $pattern = '/^[A-Z][a-z]+$/'; if (!preg_match($pattern, $last)) { echo "<p>$last is invalid.</p>"; }

  13. Regular Expressions • Name: Charles Frank or Charles E. Frank $pattern = '/^[A-Z][a-z]+ ([A-Z]\. )?[A-Z][a-z]+ $/'; if (!preg_match($pattern, $name)) { echo "<p>$name is invalid.</p>"; }

More Related