★ wanayoo — archive 1999 http://fr.php.net/manual/cs/function.str-pad.phpNouvelle recherche | Portail wanayoo
PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links 
search for in the  
previoussscanfstr_repeatnext
Last updated: Sun, 01 Sep 2002
view the printer friendly version or the printer friendly version with notes or change language to English | Brazilian Portuguese | Chinese | Dutch | Finnish | French | German | Hungarian | Italian | Japanese | Korean | Polish | Romanian | Russian | Spanish | Swedish | Turkish

str_pad

(PHP 4 >= 4.0.1)

str_pad -- Doplnit řetězec jiným řetězcem na určitou délku

Popis

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

str_pad() doplní řetězec input zleva, zprava nebo z obou stran na danou délku. Pokud jí není předán volitelný argument pad_string, doplní se input mezerami, jinak se doplní znaky z pad_string.

Volitelný argument pad_type může nabýt hodnot STR_PAD_RIGHT, STR_PAD_LEFT nebo STR_PAD_BOTH. Default je STR_PAD_RIGHT.

Pokud je hodnota pad_length negativní nebo menší než je délka input, k doplnění nedojde.

Příklad 1. Ukázka str_pad()

$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
add a note about notes
rob@OhReally.com
03-Mar-2001 10:34

To reformat the date "3-3-2001" to "2001-03-03" you would use this regexp:

$newdate = preg_replace("/([0-9]+)-([0-9]+)-([0-9]+)/ie", "'\\3-'.str_pad('\\2', 2, '0', STR_PAD_LEFT).'-'.str_pad('\\1', 2, '0', STR_PAD_LEFT)", $date);

matthew.somerville@trinity.oxford.ac.uk
05-Aug-2001 01:53

I find:

$mystr = substr("0000$mystr",-4)

(where the 0s are padding characters and -4 is the negative of the maxlength to pad to) is even simpler.

jared@dctkc.com
03-May-2002 11:25

matthew.somerville's code did NOT work for me.
neither did bert@catsburg.com's.
neither did any other documentation.
(if I isolated the code by itself, they all worked; but in the context I was using it, $value was numeric, and there is NO WAY to force it to be a string in PHP).

I had to roll my own. I used 'trim' to _force_
the value of $value to be string, because as
long as PHP thought it was numeric, I could
not use any form of left-padding with zeros.

This worked (I use sprintf to left-pad
$value with zeros to a length of 4):

sprintf("%04s",trim($value));

no_email@php.net
30-May-2002 05:34

To convert your string couldnt you just use strval($var)?

What if you want non-breaking spaces? If I try to str_pad with " " PHP thinks my padding string is 6 characters, whn in HTML it is only one.

pestilenc@hotmail.com
06-Jun-2002 11:26

For me this worked.
$string = 'help';

#First, str_pad() with unique character.
$string = str_pad($string, 10, "*", STR_PAD_BOTH);
#$string = '***help***';

#Second, str_replace with ' '
$string = str_replace("*", " ", $string);

cj@NOSPAM.ceejayoz.com
17-Jun-2002 07:38

Another solution to no_email@php.net's problem would be to simply multiply the padding string by 6.

e.g.

$string = str_pad($string, 10, " ", STR_PAD_BOTH);

becomes

$string = str_pad($string, 60, " ", STR_PAD_BOTH);

which will avoid problems with the above solution, which will mess up text that has the * in it (or whichever character you're replacing)

pozytron#mac#com
08-Jul-2002 09:20

The trouble with cj@NOSPAM.ceejayoz.com's version over pestilenc@hotmail.com's is that many of the times it is used, it will end up with fragments of the " " code.

For example, the following code:
<?php
echo str_pad("TESTSTRING",60,"&nbsp;",STR_PAD_BOTH);
?>

produces the following HTML output:
&nbsp;&nbsp;&nbsp;&nbsp;&TESTSTRING&nbsp;&nbsp;&nbsp;&nbsp;&

saveloywill@netscape.net
10-Aug-2002 07:50

Here is my solution to the above problem.
Simply place the code in question inbetween html <pre> tags, like this:

<?php
print "<pre>";
$input = "Alien";
print str_pad($input, 10);
print "*";
print "</pre>";
?>

Thanks.

mreilly@NOSPAM.mac.com
20-Aug-2002 06:23

When provided with a string of characters as the pad value, str_pad uses all the characters as fill, and can leave partial strings. (eg. If the pad value is 'ABC' and it needs 5 characters to pad with, it outputs 'ABCAB'.) This is a problem when you want to pad with non-breaking spaces, the code for which is 6 characters long.

This can be resolved by first padding the string with a single character that won't be found in the strings such as * then doing a str_replace of * with &nbsp;.

cd579@hotmail.com
22-Aug-2002 08:53

Why not just use a space[' '] instead of a character that COULD be inside the string, and risk messing it up? Example:

$string = 'test';

$string = str_pad($string, 10, " ", STR_PAD_BOTH);
// $string now equals ' test'

$string = str_replace(" ", "&nbsp;", $string);

add a note about notes
previoussscanfstr_repeatnext
Last updated: Sun, 01 Sep 2002
show source | credits | stats | mirror sites
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by: nexen.net
Last updated: Fri Sep 6 04:46:32 2002 CEST