★ wanayoo — archive 1999 http://php.net/manual/it/function.str-pad.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Strings
Quick Reference
English version of this pageGerman version of this pageJapanese version of this page
Italian version of this pageFrench version of this pageHungarian version of this page
Spanish version of this pageDutch version of this pageBrazilian Portuguese version of this page
Korean version of this pageCzech version of this page
Strings
* AddCSlashes
* AddSlashes
* bin2hex
* Chop
* Chr
* chunk_split
* convert_cyr_string
* count_chars
* crc32
* crypt
* echo
* explode
* get_html_translation_table
* get_meta_tags
* hebrev
* hebrevc
* htmlentities
* htmlspecialchars
* implode
* join
* levenshtein
* ltrim
* md5
* Metaphone
* nl2br
* Ord
* parse_str
* print
* printf
* quoted_printable_decode
* quotemeta
* rtrim
* sscanf
* setlocale
* similar_text
* soundex
* sprintf
* strcasecmp
* strchr
* strcmp
* strcspn
* strip_tags
* stripcslashes
* stripslashes
* stristr
* strlen
* strnatcmp
* strnatcasecmp
* strncmp
* str_pad
* strpos
* strrchr
* str_repeat
* strrev
* strrpos
* strspn
* strstr
* strtok
* strtolower
* strtoupper
* str_replace
* strtr
* substr
* substr_count
* substr_replace
* trim
* ucfirst
* ucwords
* wordwrap

  Thanks to:
 Chek.com
 easyDNS
 VA Linux Systems

  Related sites:
Apache
MySQL
PostgreSQL
Zend Tech.

  Community:
LinuxFund.org
OSDN
Manual: str_pad
View the source code for this pageSearch the site



Previous page
 strncmp
 Updated
Mon, 12 Feb 2001
strpos 
Next page


str_pad

(PHP 4 >= 4.0.1)

str_pad -- Pad a string to a certain length with another string

Description

string str_pad (string input, int pad_length [, string pad_string [, int pad_type]])

This functions pads the input string on the left, the right, or both sides to the specifed padding length. If the optional argument pad_string is not supplied, the input is padded with spaces, otherwise it is padded with characters from pad_string up to the limit.

Optional argument pad_type can be STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH. If pad_type is not specified it is assumed to be STR_PAD_RIGHT.

If the value of pad_length is negative or less than the length of the input string, no padding takes place.

Esempio 1. str_pad() example


$input = "Alien";
print str_pad($input, 10);                      // produces "Alien     "
print str_pad($input, 10, "-=", STR_PAD_LEFT);  // produces "-=-=-Alien"
print str_pad($input, 10, "_", STR_PAD_BOTH);   // produces "__Alien___"
      


User Contributed Notes: str_pad


philip@thepr()jects.()rg
12-Dec-2000 03:13
According to change log, the following applies to 4.0.2 :

Updated str_pad() to be able to pad on left/right/both sides.



misc.anders@feder.dk
28-Dec-2000 05:34
In earlier versions of PHP you want to declare a function like:

function str_pad($String, $Lenght) {
if (strlen($String) < $Lenght) {
$String .= str_repeat(" ", $Lenght - strlen($String));
}
return $String;
}



kno@aon.at
11-Jan-2001 04:27
str_pad for earlier Versions:
$pm....string to pad
$lt....desired length
$pw....charakter to pad with
$dn....direction
-> 0..from the left
-> 1..from the right
-> 2..from both sides

function str_pd($pm, $lt, $pw, $dn){
$cnt = $lt - strlen($pm);
$for($i=0;$i<$cnt;$i++){
switch($dn){
case 2:
$i%2?$pm=$pw.$pm:$pm=$pm.$pw;
break;
case 1:
$pm=$pm.$pw;
break;
default:
$pm=$pw.$pm;
break;
}
}
return $pm;
}



 About Notes


Previous page
 strncmp
 Updated
Mon, 12 Feb 2001
strpos 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.