4.4. Cleaning StringsOften, the strings we get from files or users need to be cleaned up before we can use them. Two common problems with raw data are the presence of extraneous whitespace, and incorrect capitalization (uppercase versus lowercase). 4.4.1. Removing WhitespaceYou can remove leading or trailing whitespace with the trim( ), ltrim( ), and rtrim( ) functions:
TRim( ) returns a copy of string with whitespace removed from the beginning and the end. ltrim( ) (the l is for left) does the same, but removes whitespace only from the start of the string. rtrim( ) (the r is for right) removes whitespace only from the end of the string. The optional charlist argument is a string that specifies all the characters to strip. The default characters to strip are given in Table 4-3.
For example:
Given a line of tab-separated data, use the charset argument to remove leading or trailing whitespace without deleting the tabs:
4.4.2. Changing CasePHP has several functions for changing the case of strings
If you've got a mixed-case string that you want to convert to "title case," where the first letter of each word is in uppercase and the rest of the letters are in lowercase (and you are not sure what case the string is in to begin with), use a combination of strtolower( ) and ucwords( ):
|
Wednesday, October 21, 2009
Section 4.4. Cleaning Strings
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment