PHP Strings Primer - Strings Formatting (Page 11 of 37 ) As we just saw with the 'printf()' function, formatting can produce clean, consistent output. Formatting functions also provide methods to ensure our basic string comparisons work and they supply us with ways to store our data in a constant fashion. Luckily, PHP provides a vast array of formatting functions. Let's take a high level overview of several formatting functions, and then we will go into specific uses of each. strtolower(string) -This function will convert any given string to lowercase. This can be useful when comparing strings to make sure that the cases are the same.strtoupper '(string) '-This is the complement to the 'strtolower()' function.ucfirst(string) -Any string passed to this function will have the first character capitalized.ucwords(string) -This acts very similar to the 'ucfirst()' function, except that the first character of all words in the string are capitalized.strrev(string) -This function will reverse a string.ltrim(string) -Whitespace at the beginning of a string will be stripped with this function.rtrim(string) -Same as 'ltrim', except it strips whitespace from the end of the string. This function also goes by the name chop()'.trim(string) -This function combines the functionality of 'ltrim' and 'rtrim' and strips whitespace from both the beginning and the end of a string.str_pad(input, pad_length, [,pad_string] [,pad_type]) -With this function, we can pad a string in a variety of ways.str_repeat(input, multiplier) -Given a string and a number of times to display it, this function will repeat the string the given amount of times.nl2br(string) -This function will insert HTML line breaks before each newline in a string.wordwrap(string, [,width [,break [,cut]]]) -We can produce output that is wrapped (or broken) at a certain width. Optionally, we can specify what character to use for the line breaks.chunk_split(string, [,chunklen] [,end]) -This function is very similar to the wordwrap function, with only minor differences. Next: Preparing user input for comparisons >>
More Programming Basics Articles More By Matt Wade |