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