PHP/String/str pad
Содержание
- 1 Make use of str_pad()"s optional parameters
- 2 string str_pad ( string input, int length [, string padding [, int type]] )
- 3 str_pad-2.php
- 4 str_pad() function pads string to length pad_length with a specified characters
- 5 STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH:
- 6 str_pad.php
- 7 There is an optional third parameter to str_pad( ) that lets you set the padding character to use
- 8 Using left space padding
- 9 Using left zero padding
Make use of str_pad()"s optional parameters
<?
$header = "Table of Contents";
print str_pad ($header, 5, "=-", STR_PAD_BOTH);
?>
string str_pad ( string input, int length [, string padding [, int type]] )
<?
$string = "Goodbye, Perl!";
$newstring = str_pad($string, 2);
?>
str_pad-2.php
<?php
$header = "Log";
echo str_pad ($header, 20, "=+", STR_PAD_BOTH);
?>
str_pad() function pads string to length pad_length with a specified characters
Its syntax is: string str_pad (string input, int pad_length [, string pad_string [, int pad_type]])
If pad_string is not specified, string will be padded with blank spaces.
pad_type may be assigned STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH
This example shows how to pad a string using str_pad() defaults:
<?
$food = "salad";
print str_pad ($food, 5);
?>
STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH:
<?
$string = "Goodbye, Perl!";
$a = str_pad($string, 10, "-", STR_PAD_LEFT);
$b = str_pad($string, 10, "-", STR_PAD_RIGHT);
$c = str_pad($string, 10, "-", STR_PAD_BOTH);
?>
str_pad.php
<?php
echo str_pad("Salad", 10)." is good.";
?>
There is an optional third parameter to str_pad( ) that lets you set the padding character to use
<?
$string = "Goodbye, Perl!";
$newstring = str_pad($string, 10, "a");
?>
Using left space padding
<?php
printf("Space padding can be tricky in HTML % 5d.", 42);
?>
Using left zero padding
<?php
printf("Zero padding can help alignment %05d.", 42);
?>