Finding the Length of a String The strlen Function To find the length of a string how many characters there are in the string, PHP provides the strlen function... Finding the Number of
Trang 1Figure 6.6 Precision of numbers Output from Example 6.6
In the next example, printf() will format a string and a number
Trang 2Figure 6.7 Output from Example 6.7
Table 6.2 shows the most common format specifiers
The format specifier can be modified by placing specifying a precision, left or right justification, padding characters, and so on, as shown in Table 6.3
Table 6.3 Modifiers for the printf() Format Specifier
Modifier Example Format
The sprintf() Function
This function is identical to printf() except that instead of displaying the formatted string, sprintf() returns the formatted string so that you can assign it to a variable See Example 6.8
Trang 3Example 6.8
<?php
$product_name = "Purple Dress";
$product_price = 199.95;
1 $output = sprintf( "Product <b>%s</b> will cost <u>$%6.2f</u> +
tax", $product_name, $product_price );
$output
Figure 6.8 The sprintf() function Output from Example 6.8
Trang 4The fprintf() Function
Whereas the printf() function writes the output to the standard output stream (the browser), the fprintf() function sends the output to any output stream specified, usually a file
Format
int fprintf ( resource handle, string format [, mixed args [, mixed ]] )
Example:
sprintf($filehandle, "%04d-%02d-%02d", $year, $month, $day);
For more information on streams and files, see Chapter 11, “Files and Directories.”
6.2.2 Formatting Numbers and Money
Putting commas or spaces in numbers or printing out the dollar value of money causes a number to become a string and can be handled with printf() PHP also provides two special functions, the number_format() function and the money_format() function
The number_format() Function
PHP provides the number_format() function to format a number with grouped thousands There are three ways to use this function You can specify no arguments, two arguments, or four arguments, but not three arguments
When only one number is specified, the number returned will be a whole number It will include commas for every group of thousands, but the fractional part will be truncated along with the decimal point If the first number after the decimal point is 5 or higher, the new number will be rounded up
If two numbers are specified, the second number will indicate the number of decimal places to format, such as two places after the decimal point for a dollar and cents amount Groups of thousands will still be comma-separated
The third way to use this function is to specify the number to format, number of decimal places, as well as the
characters to use for separating groups of thousands, as well as the decimal point This is useful for locales that use number formats different than North American formats
Example 6.9 illustrates how to use the number_format() function Figure 6.9 shows the output, three formatted numbers
Format
string number_format ( float number [, int decimals [, string dec_point, string thousands_sep]] )
Example:
$number=123456.5456 $new_string = number_format($number); // Returns: 123,457
$new_string = number_format($number, 2); // Returns: 123,456.55 $num_francais = number_format($number, 2, ',', ' '); // Returns 1 234,56
Trang 5Figure 6.9 The number_format() function The output from Example 6.9
The money_format() Function
The money_format() function formats a number as a string representing currency Because this function depends
on a C library function called strfmon(), it cannot be implemented on your system if you are using Windows This function can format money for any number of locales and comes with a large array of formatting specifications It works with negative numbers, deals with left and right precision, padding, and so on, similar to the printf() function For a complete discussion on how to use this function, see the PHP manual
Trang 66.2.3 Finding the Length of a String
The strlen() Function
To find the length of a string (how many characters there are in the string), PHP provides the strlen() function See Example 6.10
Trang 7Figure 6.10 The strlen() function Viewing the source code from Example 6.10
6.2.4 Finding the Number of Words in a String
The str_word_count() Function
The str_word_count() function returns information about the words that make up a string A word is defined as a
locale-dependent (Germany, U.S., etc.) string containing alphabetic characters, which also can contain, but not start
with ' and - characters By default, the str_word_count() function counts the number of words in a string An
optional third argument can be one of the three values shown in Table 6.4
Table 6.4 Optional Third Arguments to the str_word_count() Function
0
1
2
An optional fourth argument, charlist, allows you to add characters that will be accepted as part of a word, such as
foreign accent marks, ellipses, long dashes, or hyphens
Format
mixed str_word_count(string string [, int format [, string charlist]] )
Example:
$num_words = str_word_count("Happy New Year, to you!");
print_r(str_word_count("Solstickan såljes till förmån för barn och gamla",1, "åÅö");
Trang 86.2.5 Changing the Case of Strings
If you are validating an e-mail address or the abbreviation for a state, such as CA or MD, you might want to convert the entire string into lowercase letters before proceding, or you might want to convert just the first character in a string, as
in Mrs or Dr PHP provides functions for changing the case of the characters in a string, as shown in Table 6.5
Table 6.5 Functions That Change the Case of Strings
The strtoupper() and strtolower() Functions
The functions strtoupper() and strtolower() are used to convert the case of characters in a string from upper-
to lowercase or vice versa strtoupper() takes a string and returns a new string with every single letter capitalized strtolower() returns a new string with every character converted to lowercase
Format
string strtoupper ( string ) string strtolower ( string )
Example:
$newstring=strtoupper("merry christmas"); // returns "MERRY CHRISTMAS"
$newstring=strtolower("HAPPY NEW YEAR"); // returns "happy new year"
The ucfirst() and ucwords() Functions
If you want to change just the first character in a string to uppercase, PHP provides the ucfirst() and ucwords() functions The ucfirst() function converts the first character of a string to uppercase The ucwords() function capitalizes first letters of all the words in the string
Trang 9$text = "it rains in spain";
1 print ucfirst( $text "<br />" ); // prints: It rains in spain
2 print ucwords( $text "<br />" ); // prints: It Rains In Spain ?>
Explanation
It rains in spain ucfirst()
ucwords()
It Rains In Spain
Figure 6.11 The ucfirst() and ucwords() functions
The mb_convert_case() Function
The mb_convert_case() function is like strtolower() and strtoupper() but is not locale dependent; that
is, it bases its conversion on Unicode characters rather than just ASCII, which means letters containing the German umlaut, the Swedish ring, or French accent marks are folded (included) into case conversion To specify the case, this function provides three modes: MB_CASE_UPPER, MB_CASE_LOWER, or MB_CASE_TITLE You can also specify a supported character set to establish how the string will be encoded
Trang 10Table 6.6 Supported Character Sets
Charset Aliases Description
[1] You can use the trim() function to remove unwanted whitespace (See “The trim() Functions—trim(), ltrim(), chop, rtrim()” on page 182)
Table 6.7 Return Value from Comparison
Trang 11Table 6.8 String Comparison
The strcmp() Function (Case Sensitive)
The strcmp() function is most often used to compare two strings
Format
int strcmp ( string str1, string str2 )
Example:
$number = strcmp( "apples", "oranges");
The strcmp() function uses a lexicographical comparison algorithm to compare two strings, meaning it compares each character in the string alphabetically based on the system’s collating sequence Because PHP uses the ASCII collating sequence, an uppercase “A” is represented as decimal 65 and an uppercase “B” as decimal 66, and so on On the other hand, a lowercase “a” is 97 and a lowercase “b” is 98, and so on If you compare “A” to “a,” you can say that
“A” is less than “a” because of their numeric representation in the ASCII table; that is, 65 is less than 97
The strcmp() function returns a number less than 0 if the first string is less than second string, a number greater than
0 if the first string is greater than the second string, and 0 if they are equal
The strcmp() function is case sensitive meaning that “Dan” and “dan” are not the same If you want to ignore the case of the letters, use the strcasecmp() function discussed next See Example 6.13 to see how the strcmp() function works and its output in Figure 6.12
Trang 121 print "strcmp( '$string1', '$string2' ) outputs " strcmp(
Trang 13The strcasecmp() Function (Case Insensitive)
The strcasecmp() function works like the strcmp() function, but ignores the case of characters in strings; that is,
an uppercase “A” and a lowercase “a” are treated as equals when comparing characters
The strcasecmp() function returns a number less than 0 if the first string is less than the second string, a number greater than 0 if the first string is greater than the second string, and 0 if they are equal Example 6.14 demonstrates how the function works
Trang 14The strncasecmp() Function (Limits Character Length)
This strncasecmp() function is similar to strcasecmp() in that it also ignores the case of characters when doing the comparison, but in addition, it lets you specify the (upper limit of the) number of characters (length) from each string to be used in the comparison
The strncasecmp() function returns a number less than 0 if the first string is less than the second string, a number greater than 0 if the first string is greater than the second string, and 0 if they are equal Example 6.15 demonstrates how this function works
Trang 15The strnatcmp() Function (Natural Order Comparison)
If you compare numeric strings, the expression '2' > '100' will evaluate to true because in the first position 2 is greater than 1 when using the ASCII collating sequence The other character positions are irrelevant because the first string only has one character The string comparison functions we have seen so far always cast their arguments to strings before doing the comparison The strnatcmp() function takes into consideration strings that contain
numbers This function compares characters in two strings using the ASCII collating sequence, but if there are any numbers within the string they are compared in natural order; that is, as numbers the way we think of numbers, where
100 is greater than 2 This is true even if the numbers occur in the middle of the string Thus 'January 2' will evaluate to less than 'January 10', whereas in a normal string comparison it would be greater since 2 is greater than 1
The strnatcasecmp() function is just like the strnatcmp() function except that it is not case insensitive when comparing strings
Format
int strnatcmp ( string str1, string str2 )
Example:
// Returns 1 string 2 > string 1 echo strnatcmp('January 2, 2006', 'January
10, 2006'); // Returns -1 string 1 > string 2 echo strcmp( 'January 2, 2006', 'January 10, 2006' );
The strspn() Function (Using a Mask for Comparison)
The strspn() function compares two strings and returns the number of characters that are contained in the initial part
of the first string that match a set of characters provided in the second string, called the mask For example, if you want
to check that a password contains both digits and letters or if a zip code consists of only numbers, this function can be used to check that specified characters are included in the string
The two optional arguments allow you define where you want to start looking for the characters in the string and the length of the string to compare Example 6.16 demonstrates how to use the strspn() function
Format
int strspn ( string str1, string str2 [, int start [, int length]] )
Example:
$year = "1953 was a very good year!"; $mask="0123456789"
$count=strspn($year,$mask,0,4); // The string must start with 4 digits
Trang 16The strcspn() Function (Comparison Not Matching a Mask)
The strcspn() function is just like the strspn() function, but finds length of initial segment not matching the mask; that is, it returns the length of the initial segment of the first string not containing any of the characters in the second string The strcspn() function accepts two optional integer parameters that can be used to define the start position and the length of the string being compared
Format
int strcspn ( string str1, string str2 [, int start [, int length]] )
Example:
$filename = "test3"; $length=strcspn("$filename", "1234567890", 0, 4); //
Returns 4; first 4 characters should not be numbers
6.2.7 Finding Similarities in Strings
The string comparison functions previously discussed perform alphanumeric string comparisons, but what if we want to see if one string sounds or is pronounced like another or how and where the text differs in two strings? PHP provides a set of functions to find similarities or differences in strings These functions might be useful for programs that check spelling, perform database searches, or any advanced text processing
The soundex() and metaphone() Functions (Phonic Similarity)
Phonic similarity bases its comparison on whether or not two strings are homophones, that is, they sound alike Words such as “genes” and “jeans” or “morning” and “mourning” are homophones
The soundex() and metaphone() functions take a string as an argument, and return a key Soundex keys are short alphanumeric representations of a word’s English pronounciation that can be used to compare the sound in strings If the keys are the same, then the words sound the same in English After testing different words, you will see that these functions base their comparison on American English pronounciation rather than British English For example, “father” and “farther” do not sound the same in America, nor do “source” and “sauce,” or “tuba” and “tuber.”
The only obvious difference between the two functions is that metaphone() is more precise in determining which words have the same pronunciation Example 6.17 demonstrates how to use the soundex() and metaphone() functions The output is diplayed in Figure 6.15
Trang 18Figure 6.15 Homophones—words that sound the same
The similar_text() and levenshtein() Functions (Textual Similarity)
PHP provides two functions to test the similarity between the text in two strings They are the similar_text() and the levenshtein() functions
The similar_text() function calculates the similarity of two strings and returns the number of characters that are the same, allowing for additions, subtraction, and repetition It also takes an optional third parameter, containing a value that represents the percentage of similarity between the strings Example 6.18 demonstrates how the
similar_text() function is used
print "Second string: $string2\n<br />";
3 $number=similar_text("$string1", "$string2", $percent);
print "There are $number of the same characters in the two strings.\n";
4 echo "The strings are similar by "
Trang 19Explanation
2
4
Figure 6.16 The similar_text() function
The levenshtein() function is used to find the Levenshtein[2] distance (also called the edit distance) between two strings (strings cannot be longer than 255 characters) What’s that? Suppose you have two strings and you want to know how you could edit one of the strings to make it just like the other string (If you have ever used the UNIX diff command, it will give you this kind of information.) The Levenshtein distance is defined as the fewest number of insertions, substitutions, and deletions required to transform one string into another string (The function is not case sensitive.) The greater the Levenshtein distance, the more different the strings are If the distance is 0, the strings are the same (For full discussion see http://www.merriampark.com/ld.htm.) Example 6.19 demonstrates how to use the
1 $string1 = "I attended a funeral.";
$string2 = "He attended a fun rally.";
Trang 20print "First string: $string1\n<br />";
print "Second string: $string2\n<br />";
Figure 6.17 The levenshtein() function
The levenshtein() function includes the first two strings and three additional parameters that define the cost of insert, substitute, and delete operations This allows you to specify how you want scores weighted with numeric values Otherwise, all scores are given equal weight The weight or cost indicates what steps should be taken to make the strings similar; that is, should insertions or deletions be made to transform the string?
“Regular Expressions and Pattern Matching.” Table 6.9 provides the names of these functions, what they do, and where
to find a complete discussion and examples
Trang 21Table 6.9 PHP Functions for Splitting Strings
The strtok() Function
The strtok() function splits a string into smaller strings called tokens Tokens are created by choosing a character(s) that will be used as a string delimiter Most tokens are words delimited by spaces, as in any typical sentence For example, “I love you.” is a space-delimited string consisting of three tokens
The first time you call strtok(), you use two arguments: the string (the str argument) that will be tokenized, and the delimiters you will use as separators (the token argument) The first tokenized string will be returned The next time you call the function, you don’t use the string argument because strtok() keeps track of where it is in the string, token by token until it reaches the end of the string If you want to start tokenizing over again, then you will use both the string and its delimiters to initialize the process
This function might return boolean FALSE, but might also return a non-boolean value that evaluates to FALSE, such as
0 or "" Example 6.20 demonstrates how to use the strtok() function to split up a string The results are displayed
Trang 23Figure 6.18 Splitting up a string with the strtok() function
print str_repeat("-", 30); // prints 30 dashes
6.2.10 Trimming and Padding Strings
PHP provides a set of functions that trim and pad strings Trimming means to remove excess or to prune, as in trimming your hair, your hedges, or your budget You might want to clean up a string by getting rid of excess whitespace (or some other character) from the beginning or end of the string
Padding a string is the opposite of trimming it If you pad your wallet or your shirt, you add more to it To pad a string means to add surrounding characters to either or both sides of the string
The trim() Functions—trim(), ltrim(), chop(), rtrim()
The trim() function strips whitespace (or other characters) from the beginning and end of a string If a third argument
is given, called the character list (charlist), you can specify the range of characters you want to remove from the string You can also use the trim() function to trim array values (see Chapter 8, “Arrays”)
Trang 24The ltrim() and rtrim() functions work just like trim(), except ltrim() only trims the left side of a string and the rtrim() function trims the right side of the string The alias for rtrim() is chop() Without the second parameter, the trim() function will strip the whitespace characters listed in Table 6.10
Table 6.10 Whitespace Characters
$trimmed_string= trim( "\t\tHello\n"); // Removes tabs and newline
$trimmed_string=ltrim("\t\tHello\n"); // Removes two tabs on the left
$trimmed_string=rtrim("\t\tHello\n"); // Removes newline on the right
If you want to specify characters other than whitespace, you can list all characters in the second optional argument, charlist With you can specify a range of characters
Example:
// Removes all asterisks $trimmed_string = trim("****Hello*****", "*"); //
Removes asterisks on the left $trimmed_string = ltrim("****Hello*****", "*"); // Removes asterisks on the right $trimmed_string = rtrim("****Hello*****", "*");
echo "Original: $text<br />";
3 echo "Modified: ", trim($text), "<br />";
echo "<hr>";
Trang 254 $text = "Cutting away the excess dots! ";
echo "Original: $text<br />";
5 echo "Modified: ", rtrim($text, ".");
Figure 6.19 Trimming a string of excess characters Output from Example 6.21
The str_pad() Function
You can pad a wallet or pad a cell Similarly, to pad a string means to lengthen it by adding a specified number of characters to the string PHP provides the str_pad() function to pad strings; the default is to pad with spaces on the right side of the string The first argument is the string to be padded, the second argument, pad_length, is the width
of the string with the padding characters If a third argument is provided, you can specify whether the padding will