Page 137 - Beginning PHP 5.3
P. 137
Chapter 5: Strings
PHP also features another handy function, money_format() , that you can use to format monetary
values according to various currency conventions, using a syntax similar to printf() . The only
drawback is that it ’ s not available on Windows platforms (at least at the time of writing). See http://
www.php.net/money_format for more details.
Summary
In this chapter you explored strings in PHP, and looked at some of the functions that you can use to
manipulate strings. You learned how to create string literals within your PHP code by using single and
double quotation marks as well as the heredoc and nowdoc syntaxes. You also learned how to find the
length of a string, as well as count the number of words in a string, access the individual characters
within a string, and access groups of characters in a string.
You then looked at various functions for searching strings and replacing text within strings, including:
❑ strstr() , strpos() , and strrpos() for searching for text
❑ substr_count() for counting the occurrences of a search term within a string
❑ strpbrk() for searching for any one of a set of characters
❑ str_replace() for replacing all occurrences of a search term within a string
❑ substr_replace() for replacing a specified portion of a string
❑ strtr() for replacing certain characters in a string with other characters
Next, you took a look at issues regarding case sensitivity, and explored a few functions — strtolower() ,
strtoupper() , ucfirst() , lcfirst(), and ucwords() — that you can use to convert case.
Finally, y ou studied PHP ’ s printf() and sprintf() functions, which you can use to format strings in
many different ways, and also learned about some other string formatting functions such as trim() ,
ltrim() , rtrim() , str_pad() , wordwrap() , and number_format() .
You now have a pretty good understanding of how strings work in PHP, and you ’ ve learned about some
of the more important string - manipulation functions that PHP offers. However, as mentioned at the start of
the chapter, PHP has a lot more string - related functions than those listed here. For a full list, see the PHP
manual at http://www.php.net/manual/en/ref.strings.php .
In the next chapter you explore another important PHP language concept: arrays. Before leaving this
chapter, though, you might find it helpful to work through the following two exercises to test your
knowledge of strings. You can find the solutions to these exercises in Appendix A.
99
9/21/09 8:53:49 AM
c05.indd 99
c05.indd 99 9/21/09 8:53:49 AM