Page 118 - Open Soource Technologies 304.indd
P. 118

Web Technologies-I



                   Notes         For example, to build a crude horizontal rule:
                                 echo str_repeat(‘-’, 40);
                                 The str_pad( ) function pads one string with another. Optionally, you can say what string to pad
                                 with, and whether to pad on the left, right, or both:
                                 $padded = str_pad(to_pad, length [, with [, pad_type ]]);

                                 The default is to pad on the right with spaces:
                                 $string = str_pad(‘Pradip Kumar’, 30); echo “$string:35:Riya”; Pradip Kumar :35:Riya
                                 The optional third argument is the string to pad with:

                                 $string = str_pad(‘Pradip Kumar’, 30, ‘. ‘); echo “{$string}35”; Pradip Kumar. . . . . . . .35
                                 The optional fourth argument can be either STR_PAD_RIGHT (the default), STR_PAD_LEFT, or
                                 STR_PAD_BOTH (to centre). For example:
                                 echo ‘[‘ . str_pad(‘Pradip Kumar’, 30, ‘ ‘, STR_PAD_LEFT) . “]\n”; echo ‘[‘ . str_pad(‘Pradip Kumar’,
                                 30, ‘ ‘, STR_PAD_BOTH) . “]\n”; [ Pradip Kumar] [ Pradip Kumar ]

                                 5.7.3 Decomposing a String
                                 PHP provides several functions to let you break a string into smaller components. In increasing
                                 order of complexity, they are explode( ), strtok( ), and sscanf( ).
                                 Exploding and Imploding
                                 Data often arrives as strings, which must be broken down into an array of values. For instance,
                                 you might want to separate out the comma-separated fields from a string such as”Pradip,25,Riya”.
                                 In these situations, use the explode( ) function:
                                 $array = explode(separator, string [, limit]);
                                 The first argument, separator, is a string containing the field separator. The second argument,
                                 string, is the string to split. The optional third argument, limit, is the maximum number of values
                                 to return in the array. If the limit is reached, the last element of the array contains the remainder
                                 of the string:
                                 $input = ‘Pradip, 25, Riya’; $fields = explode (‘,’, $input); // $fields is array (‘Pradip’, ‘25’, ‘Riya’)
                                 $fields = explode (‘,’, $input, 2); // $fields is array (‘Pradip’, ‘25, Riya’)
                                 The implode ( ) function does the exact opposite of explode ( )—it creates a large string from an
                                 array of smaller strings:
                                 $string = implode (separator, array);
                                 The first argument, separator, is the string to put between the elements of the second argument,
                                 array. To reconstruct the simple comma-separated value string, simply say:
                                 $fields = array(‘Pradip’, ‘25’, ‘Riya’); $string = implode(‘,’, $fields); // $string is ‘Pradip,25,Riya’

                                 The join( ) function is an alias for implode( ).
                                 Tokenizing
                                 The strtok( ) function lets you iterate through a string, getting a new chunk (token) each time.
                                 The first time you call it, you need to pass two arguments: the string to iterate over and the token
                                 separator:
                                 $first_chunk = strtok(string, separator);





        112                               LOVELY PROFESSIONAL UNIVERSITY
   113   114   115   116   117   118   119   120   121   122   123