Académique Documents
Professionnel Documents
Culture Documents
String Syntax
Single quotes: a string
String Operators
Array-like character access:
Concatenating Assignment : .=
Variables in Strings
Simple string with a $var in it\n You can use $an_array[$var] too\n Sometimes you need ${curl}ies to mark
where the {$var}iable ends Curlies help on {$big[fancy][$stuff]} too Where its confusing to embed . $big[ugly][$var].iables, break it up as needed with concatenation.
January 18, 2005 UPHPU - Mac Newbold 4
$take,more than one,argument; Echo shortcut: <b><?=$foo?></b> trim, ltrim, rtrim/chop remove whitespace explode, implode/join
many of them have easier alternatives Gotcha: Some of them (like strpos and friends) return boolean false, because 0 is a valid result. Always use ===false.
January 18, 2005 UPHPU - Mac Newbold 6
and in more complex cases, can only be done with regular expressions
But regular expressions are slower (more later)
str_replace(bar,baz,foobar); str_repeat(1234567890,8);
January 18, 2005 UPHPU - Mac Newbold 7
Formatting functions
strtolower, strtoupper ucfirst, ucwords uppercase first char, or
first char of each word wordwrap wrap text to a given width str_pad(tooshort,15, ); vprintf, vfprintf, vsprintf formatted output number_format add thousands grouping money_format format as currency
January 18, 2005 UPHPU - Mac Newbold 8
Special-Purpose Functions
One of PHPs strengths is the way it caters
to the common things people need Many string functions are specifically for use with things like dates/times, URLs, HTML, and SQL databases Advice: When you need them, use them. Rolling your own doesnt usually work out the way you plan it.
January 18, 2005 UPHPU - Mac Newbold 9
PCRE POSIX
10
Regular Expressions
Extremely powerful tool for pattern
matching same thing used by compilers and interpreters to run your programs Two flavors in PHP:
Basics of REs
They match patterns the magic is in the
pattern you tell them to match They have to be precise, including and excluding exactly what you want People get scared of them because the details can be tricky But theyre one of the best tools you have for doing some pretty fancy string stuff
12
RE Patterns
Start with strings and grouping: abc(def) Add alternative branches: abc(def|123) Wildcard: . matches any char but \n Quantifiers/Repeating:
(abc)+(def|123)*(.{2})*
If you want to use -, put it at the beginning Escape any special chars with \ as usual If first char is ^, class is negated
\d = [0-9], \D = [^0-9] \s = whitespace, \S = non-whitespace \w = [a-zA-Z0-9_], \W = [^a-zA-Z0-9_] \b = word boundary zero-width assertion
14
Anchors
What if you want to force it to match only at
the beginning of the string? Or to match the entire string? Use an anchor! ^ as the first char anchors the beginning $ as the last char anchors the end (Varies slightly in multi-line mode)
15
Theyll keep eating characters as long as they can keep matching. Consider: <.*> vs. <[^>]*> when matching against <b>Hi</b>
/i = case insensitive /U = un-greedy /m = multi-line
16
Back References
Most commonly used in replace operations,
but can be used in match patterns as well Parentheses not only group, but capture too Use \ followed by the number of the capture ab(.)\1(.)\2 will match abccdd or abxxyy, but not abcccd or abdcdc Can get tricky to count which backref goes where with nested parentheses
17
Can simplify back-reference counting They dont advance the matching position Positive: (?= ), or Negative: (?! ) Very powerful, but not always easy to understand. Trial and error can be your friend!
18
Look-ahead Assertions:
PCRE Specifics
www.php.net/pcre preg_match, preg_match_all, preg_replace,
preg_split, preg_grep (filter an array) Perl REs have a delimiter, usually /, but can be anything:
preg_match(/foo/,$bar); preg_match(%/usr/local/bin/%,$path);
19
POSIX Specifics
www.php.net/regex ereg, ereg_replace, split, eregi, spliti, etc. [Only] Advantage over PCRE: It doesnt
require the PCRE library to be installed, so its always there in any PHP installation Other regex engines support this specification, though the Perl style seems to be more popular.
20
Almost there
Intro to Strings in PHP
PCRE POSIX
Performance/Speed
Rule of thumb: use the simplest function
that will get the job done right
strpos instead of substr str_replace instead of preg_replace And so forth The PHP manual online usually includes notes about speed differences
Grab Bag
md5, md5_file Calculate md5 hashes
similarity of two strings metaphone, soundex calculate how similar two strings sound when spoken out loud str_rot13 Encryption algorithm
Grab Bag 2
str_shuffle words are much more fun once
theyve been randomized count_chars, str_word_count statistics about your strings str_rev if it doesnt make sense forward, try it backwards
24