1.12.1. ProblemYou need to format data records such that each field takes up a set amount of characters. 1.12.2. SolutionUse pack( ) with a format string that specifies a sequence of space-padded strings. Example 1-32 transforms an array of data into fixed-width records. Generating fixed-width field data records
1.12.3. DiscussionThe format string A25A14A4 tells pack( ) to transform its subsequent arguments into a 25-character space-padded string, a 14-character space-padded string, and a 4-character space-padded string. For space-padded fields in fixed-width records, pack( ) provides a concise solution. To pad fields with something other than a space, however, use substr( ) to ensure that the field values aren't too long and str_pad( ) to ensure that the field values aren't too short. Example 1-33 transforms an array of records into fixed-width records with .-padded fields. Generating fixed-width field data records without pack( )
1.12.4. See AlsoDocumentation on pack( ) at http://www.php.net/pack and on str_pad( ) at http://www.php.net/str_pad. Recipe 1.16 discusses pack( ) format strings in more detail. |