PHP -str_pad()- Funktion
Beispiel
Füllen Sie die rechte Seite der Zeichenfolge auf eine neue Länge von 20 Zeichen auf:
<?php
$str = "Hello World";
echo str_pad($str,20,".");
?>
Definition und Verwendung
Die Funktion str_pad() füllt einen String auf eine neue Länge auf.
Syntax
str_pad(string,length,pad_string,pad_type)
Parameterwerte
Parameter | Description |
---|---|
string | Required. Specifies the string to pad |
length | Required. Specifies the new string length. If this value is less than the original length of the string, nothing will be done |
pad_string | Optional. Specifies the string to use for padding. Default is whitespace |
pad_type | Optional. Specifies what side to pad the string. Possible values:
|
Technische Details
Rückgabewert: | Gibt die aufgefüllte Zeichenfolge zurück |
---|---|
PHP-Version: | 4.0.1+ |
Mehr Beispiele
Beispiel
Pad auf der linken Seite der Saite:
<?php
$str = "Hello World";
echo str_pad($str,20,".",STR_PAD_LEFT);
?>
Beispiel
Pad auf beiden Seiten der Saite:
<?php
$str = "Hello World";
echo str_pad($str,20,".:",STR_PAD_BOTH);
?>
❮ PHP-String-Referenz