By default, the string will be padded to the right; however, the optional parameter
pad_type may be assigned the values STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH,
padding the string accordingly. This example shows how to pad a string using str_pad():
echo str_pad("Salad", 10)." is good.";
?>
This returns the following:
Salad is good.
This example makes use of str_pad()??™s optional parameters:
$header = "Log Report";
echo str_pad ($header, 20, "=+", STR_PAD_BOTH);
?>
This returns the following:
=+=+=Log Report=+=+=
CHAPTER 9 ?– S TRINGS AND REGULAR EXPRESS IONS 271
Note that str_pad() truncates the pattern defined by pad_string if length is reached
before completing an entire repetition of the pattern.
Counting Characters and Words
It??™s often useful to determine the total number of characters or words in a given string.
Although PHP??™s considerable capabilities in string parsing has long made this task
trivial, two functions were recently added that formalize the process. Both functions
are introduced in this section.
Counting the Number of Characters in a String
The function count_chars() offers information regarding the characters found in a
string. Its prototype follows:
mixed count_chars(string str [, mode])
Its behavior depends on how the optional parameter mode is defined:
0: Returns an array consisting of each found byte value as the key and the corresponding
frequency as the value, even if the frequency is zero.
Pages:
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346