1--TEST-- 2str_pad() function 3--INI-- 4precision=14 5--FILE-- 6<?php 7/* Pad a string to a certain length with another string */ 8 9echo "\n#### Basic operations ####\n"; 10$input_string = "str_pad()"; 11$pad_length = 20; 12$pad_string = "-+"; 13var_dump( str_pad($input_string, $pad_length) ); // default pad_string & pad_type 14var_dump( str_pad($input_string, $pad_length, $pad_string) ); // default pad_type 15var_dump( str_pad($input_string, $pad_length, $pad_string, STR_PAD_LEFT) ); 16var_dump( str_pad($input_string, $pad_length, $pad_string, STR_PAD_RIGHT) ); 17var_dump( str_pad($input_string, $pad_length, $pad_string, STR_PAD_BOTH) ); 18 19echo "\n#### variations with input string and pad-length ####\n"; 20/* different input string variation */ 21$input_strings = array( 22 "variation", // normal string 23 "", // empty string 24 true, // boolean 25 15, // numeric 26 15.55, // numeric 27 "2990" // numeric string 28); 29 30/* different pad_lengths */ 31$pad_lengths = array( 32 -PHP_INT_MAX, // huge negative value 33 -1, // negative value 34 0, // pad_length < sizeof(input_string) 35 9, // pad_length <= sizeof(input_string) 36 10, // pad_length > sizeof(input_string) 37 16, // pad_length > sizeof(input_string) 38); 39 40$pad_string = "="; 41/* loop through to use each variant of $pad_length on each element of $input_strings array */ 42foreach ($input_strings as $input_string ) { 43 foreach ($pad_lengths as $pad_length ) { 44 var_dump( str_pad($input_string, $pad_length) ); // default pad_string & pad_type 45 var_dump( str_pad($input_string, $pad_length, $pad_string) ); // default pad_type 46 var_dump( str_pad($input_string, $pad_length, $pad_string, STR_PAD_LEFT) ); 47 var_dump( str_pad($input_string, $pad_length, $pad_string, STR_PAD_RIGHT) ); 48 var_dump( str_pad($input_string, $pad_length, $pad_string, STR_PAD_BOTH) ); 49 } 50} 51 52echo "\n#### variation with pad string ####\n"; 53$pad_strings = array ("=", 1, true, "string_pad", 1.5, "\t", '\t'); 54$input_string="variation"; 55$pad_length = 16; 56foreach ( $pad_strings as $pad_string ) { 57 var_dump( str_pad($input_string, $pad_length, $pad_string) ); // default pad_type 58 var_dump( str_pad($input_string, $pad_length, $pad_string, STR_PAD_LEFT) ); 59 var_dump( str_pad($input_string, $pad_length, $pad_string, STR_PAD_RIGHT) ); 60 var_dump( str_pad($input_string, $pad_length, $pad_string, STR_PAD_BOTH) ); 61} 62 63echo "\n#### error conditions ####\n"; 64 65echo "\n--- empty padding string ---\n"; 66 67try { 68 str_pad($input_string, 12, ""); 69} catch (\ValueError $e) { 70 echo $e->getMessage() . "\n"; 71} 72 73/* bad pad_type - passing an undefined one */ 74 75try { 76 str_pad($input_string, $pad_length, "+", 15); 77} catch (\ValueError $e) { 78 echo $e->getMessage() . "\n"; 79} 80 81?> 82--EXPECT-- 83#### Basic operations #### 84string(20) "str_pad() " 85string(20) "str_pad()-+-+-+-+-+-" 86string(20) "-+-+-+-+-+-str_pad()" 87string(20) "str_pad()-+-+-+-+-+-" 88string(20) "-+-+-str_pad()-+-+-+" 89 90#### variations with input string and pad-length #### 91string(9) "variation" 92string(9) "variation" 93string(9) "variation" 94string(9) "variation" 95string(9) "variation" 96string(9) "variation" 97string(9) "variation" 98string(9) "variation" 99string(9) "variation" 100string(9) "variation" 101string(9) "variation" 102string(9) "variation" 103string(9) "variation" 104string(9) "variation" 105string(9) "variation" 106string(9) "variation" 107string(9) "variation" 108string(9) "variation" 109string(9) "variation" 110string(9) "variation" 111string(10) "variation " 112string(10) "variation=" 113string(10) "=variation" 114string(10) "variation=" 115string(10) "variation=" 116string(16) "variation " 117string(16) "variation=======" 118string(16) "=======variation" 119string(16) "variation=======" 120string(16) "===variation====" 121string(0) "" 122string(0) "" 123string(0) "" 124string(0) "" 125string(0) "" 126string(0) "" 127string(0) "" 128string(0) "" 129string(0) "" 130string(0) "" 131string(0) "" 132string(0) "" 133string(0) "" 134string(0) "" 135string(0) "" 136string(9) " " 137string(9) "=========" 138string(9) "=========" 139string(9) "=========" 140string(9) "=========" 141string(10) " " 142string(10) "==========" 143string(10) "==========" 144string(10) "==========" 145string(10) "==========" 146string(16) " " 147string(16) "================" 148string(16) "================" 149string(16) "================" 150string(16) "================" 151string(1) "1" 152string(1) "1" 153string(1) "1" 154string(1) "1" 155string(1) "1" 156string(1) "1" 157string(1) "1" 158string(1) "1" 159string(1) "1" 160string(1) "1" 161string(1) "1" 162string(1) "1" 163string(1) "1" 164string(1) "1" 165string(1) "1" 166string(9) "1 " 167string(9) "1========" 168string(9) "========1" 169string(9) "1========" 170string(9) "====1====" 171string(10) "1 " 172string(10) "1=========" 173string(10) "=========1" 174string(10) "1=========" 175string(10) "====1=====" 176string(16) "1 " 177string(16) "1===============" 178string(16) "===============1" 179string(16) "1===============" 180string(16) "=======1========" 181string(2) "15" 182string(2) "15" 183string(2) "15" 184string(2) "15" 185string(2) "15" 186string(2) "15" 187string(2) "15" 188string(2) "15" 189string(2) "15" 190string(2) "15" 191string(2) "15" 192string(2) "15" 193string(2) "15" 194string(2) "15" 195string(2) "15" 196string(9) "15 " 197string(9) "15=======" 198string(9) "=======15" 199string(9) "15=======" 200string(9) "===15====" 201string(10) "15 " 202string(10) "15========" 203string(10) "========15" 204string(10) "15========" 205string(10) "====15====" 206string(16) "15 " 207string(16) "15==============" 208string(16) "==============15" 209string(16) "15==============" 210string(16) "=======15=======" 211string(5) "15.55" 212string(5) "15.55" 213string(5) "15.55" 214string(5) "15.55" 215string(5) "15.55" 216string(5) "15.55" 217string(5) "15.55" 218string(5) "15.55" 219string(5) "15.55" 220string(5) "15.55" 221string(5) "15.55" 222string(5) "15.55" 223string(5) "15.55" 224string(5) "15.55" 225string(5) "15.55" 226string(9) "15.55 " 227string(9) "15.55====" 228string(9) "====15.55" 229string(9) "15.55====" 230string(9) "==15.55==" 231string(10) "15.55 " 232string(10) "15.55=====" 233string(10) "=====15.55" 234string(10) "15.55=====" 235string(10) "==15.55===" 236string(16) "15.55 " 237string(16) "15.55===========" 238string(16) "===========15.55" 239string(16) "15.55===========" 240string(16) "=====15.55======" 241string(4) "2990" 242string(4) "2990" 243string(4) "2990" 244string(4) "2990" 245string(4) "2990" 246string(4) "2990" 247string(4) "2990" 248string(4) "2990" 249string(4) "2990" 250string(4) "2990" 251string(4) "2990" 252string(4) "2990" 253string(4) "2990" 254string(4) "2990" 255string(4) "2990" 256string(9) "2990 " 257string(9) "2990=====" 258string(9) "=====2990" 259string(9) "2990=====" 260string(9) "==2990===" 261string(10) "2990 " 262string(10) "2990======" 263string(10) "======2990" 264string(10) "2990======" 265string(10) "===2990===" 266string(16) "2990 " 267string(16) "2990============" 268string(16) "============2990" 269string(16) "2990============" 270string(16) "======2990======" 271 272#### variation with pad string #### 273string(16) "variation=======" 274string(16) "=======variation" 275string(16) "variation=======" 276string(16) "===variation====" 277string(16) "variation1111111" 278string(16) "1111111variation" 279string(16) "variation1111111" 280string(16) "111variation1111" 281string(16) "variation1111111" 282string(16) "1111111variation" 283string(16) "variation1111111" 284string(16) "111variation1111" 285string(16) "variationstring_" 286string(16) "string_variation" 287string(16) "variationstring_" 288string(16) "strvariationstri" 289string(16) "variation1.51.51" 290string(16) "1.51.51variation" 291string(16) "variation1.51.51" 292string(16) "1.5variation1.51" 293string(16) "variation " 294string(16) " variation" 295string(16) "variation " 296string(16) " variation " 297string(16) "variation\t\t\t\" 298string(16) "\t\t\t\variation" 299string(16) "variation\t\t\t\" 300string(16) "\t\variation\t\t" 301 302#### error conditions #### 303 304--- empty padding string --- 305str_pad(): Argument #3 ($pad_string) must not be empty 306str_pad(): Argument #4 ($pad_type) must be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH 307