Professional Documents
Culture Documents
Using PHP Strings
Using PHP Strings
Using PHP Strings
Using Strings
Note
There are many PHP functions available for arrays and for
strings.
You will only be responsible for those presented in class.
You may use others you find in PHP references for your
homework assignments.
(http://www.php.net/string - string type,
http://us2.php.net/manual/en/ref.strings.php - string functions,
http://us2.php.net/manual/en/book.pcre.php - Perl-compatible
regular expressions)
Strings
Topics:
Formatting strings: trimming, for presentation, for storage
Joining and splitting strings with string functions
Comparing strings
Matching and replacing substrings with string functions
Using regular expressions
Strings
Recall:
String literals:
formatting strings
They will be familiar to C programmers
They are a bit messy and we will cover only briefly for a
Example:
printf(%4d and %5.3f, $a, $b);
If $a is 1.23 and $b is 4.56, prints 1 and 4.560
There are far too many options to describe them all here.
Read your textbook or review your Java text for more details
10
nl2br.php
http://www.nku.edu/~frank/csc301/Examples/PHP_String
s/nl2br.php
http://www.nku.edu/~frank/csc301/Examples/PHP_String
s/nl2br_php.pdf
12
if its alphabetic
string ucwords(string $str) sets 1st character in each word that
begins with an alphabetic character to upper case
For each of these functions:
The argument is a string
Function creates a new string
13
front of them: \, \ \\
how: string addslashes(string $str) returns the reformatted $str
string
Conversely:
string stripslashes(string $str) returns a copy of the $str string from
15
slashes
http://www.nku.edu/~frank/csc301/Examples/PHP_String
s/quotes.php
http://www.nku.edu/~frank/csc301/Examples/PHP_String
s/quotes_php.pdf
16
18
http://www.nku.edu/~frank/csc301/Examples/PHP_String
s/strtok.php
http://www.nku.edu/~frank/csc301/Examples/PHP_String
s/strtok_php.pdf
19
20
Comparing Strings
Use == and === for exact compare
Use strcmp() for ordered compare:
int strcmp(string $str1, string $str2)
Returns <0 if $str1 sorts before $str2 (= $str1 is less than
21
Comparing Strings
strncmp( )
Similar to strcmp(),
Has 3rd argument: number of characters to compare (if < strings
length)
22