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 chr(0).chr(255).chr(128).chr(234).chr(143), // >7-bit ASCII 24 "", // empty string 25 NULL, // NULL 26 true, // boolean 27 15, // numeric 28 15.55, // numeric 29 "2990" // numeric string 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$pad_string = "="; 41/*loop through to use each varient of $pad_length on 42 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 ####"; 65/* args less than min. expected of 2 */ 66str_pad(); 67str_pad($input_string); 68 69/* args more than expected,expected 4 */ 70str_pad($input_tring, $pad_length, $pad_string, STR_PAD_LEFT, NULL ); 71 72echo "\n--- padding string as null ---"; 73str_pad($input_string, 12, NULL); 74str_pad($input_string, 12, ""); 75 76/* bad pad_type - passing an undefined one */ 77var_dump( str_pad($input_string, $pad_length, "+", 15) ); 78 79echo "Done\n"; 80?> 81--EXPECTF-- 82#### Basic operations #### 83string(20) "str_pad() " 84string(20) "str_pad()-+-+-+-+-+-" 85string(20) "-+-+-+-+-+-str_pad()" 86string(20) "str_pad()-+-+-+-+-+-" 87string(20) "-+-+-str_pad()-+-+-+" 88 89#### variations with input string and pad-length #### 90string(9) "variation" 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(10) "variation " 111string(10) "variation=" 112string(10) "=variation" 113string(10) "variation=" 114string(10) "variation=" 115string(16) "variation " 116string(16) "variation=======" 117string(16) "=======variation" 118string(16) "variation=======" 119string(16) "===variation====" 120string(5) "���" 121string(5) "���" 122string(5) "���" 123string(5) "���" 124string(5) "���" 125string(5) "���" 126string(5) "���" 127string(5) "���" 128string(5) "���" 129string(5) "���" 130string(5) "���" 131string(5) "���" 132string(5) "���" 133string(5) "���" 134string(5) "���" 135string(9) "��� " 136string(9) "���====" 137string(9) "====���" 138string(9) "���====" 139string(9) "==���==" 140string(10) "��� " 141string(10) "���=====" 142string(10) "=====���" 143string(10) "���=====" 144string(10) "==���===" 145string(16) "��� " 146string(16) "���===========" 147string(16) "===========���" 148string(16) "���===========" 149string(16) "=====���======" 150string(0) "" 151string(0) "" 152string(0) "" 153string(0) "" 154string(0) "" 155string(0) "" 156string(0) "" 157string(0) "" 158string(0) "" 159string(0) "" 160string(0) "" 161string(0) "" 162string(0) "" 163string(0) "" 164string(0) "" 165string(9) " " 166string(9) "=========" 167string(9) "=========" 168string(9) "=========" 169string(9) "=========" 170string(10) " " 171string(10) "==========" 172string(10) "==========" 173string(10) "==========" 174string(10) "==========" 175string(16) " " 176string(16) "================" 177string(16) "================" 178string(16) "================" 179string(16) "================" 180string(0) "" 181string(0) "" 182string(0) "" 183string(0) "" 184string(0) "" 185string(0) "" 186string(0) "" 187string(0) "" 188string(0) "" 189string(0) "" 190string(0) "" 191string(0) "" 192string(0) "" 193string(0) "" 194string(0) "" 195string(9) " " 196string(9) "=========" 197string(9) "=========" 198string(9) "=========" 199string(9) "=========" 200string(10) " " 201string(10) "==========" 202string(10) "==========" 203string(10) "==========" 204string(10) "==========" 205string(16) " " 206string(16) "================" 207string(16) "================" 208string(16) "================" 209string(16) "================" 210string(1) "1" 211string(1) "1" 212string(1) "1" 213string(1) "1" 214string(1) "1" 215string(1) "1" 216string(1) "1" 217string(1) "1" 218string(1) "1" 219string(1) "1" 220string(1) "1" 221string(1) "1" 222string(1) "1" 223string(1) "1" 224string(1) "1" 225string(9) "1 " 226string(9) "1========" 227string(9) "========1" 228string(9) "1========" 229string(9) "====1====" 230string(10) "1 " 231string(10) "1=========" 232string(10) "=========1" 233string(10) "1=========" 234string(10) "====1=====" 235string(16) "1 " 236string(16) "1===============" 237string(16) "===============1" 238string(16) "1===============" 239string(16) "=======1========" 240string(2) "15" 241string(2) "15" 242string(2) "15" 243string(2) "15" 244string(2) "15" 245string(2) "15" 246string(2) "15" 247string(2) "15" 248string(2) "15" 249string(2) "15" 250string(2) "15" 251string(2) "15" 252string(2) "15" 253string(2) "15" 254string(2) "15" 255string(9) "15 " 256string(9) "15=======" 257string(9) "=======15" 258string(9) "15=======" 259string(9) "===15====" 260string(10) "15 " 261string(10) "15========" 262string(10) "========15" 263string(10) "15========" 264string(10) "====15====" 265string(16) "15 " 266string(16) "15==============" 267string(16) "==============15" 268string(16) "15==============" 269string(16) "=======15=======" 270string(5) "15.55" 271string(5) "15.55" 272string(5) "15.55" 273string(5) "15.55" 274string(5) "15.55" 275string(5) "15.55" 276string(5) "15.55" 277string(5) "15.55" 278string(5) "15.55" 279string(5) "15.55" 280string(5) "15.55" 281string(5) "15.55" 282string(5) "15.55" 283string(5) "15.55" 284string(5) "15.55" 285string(9) "15.55 " 286string(9) "15.55====" 287string(9) "====15.55" 288string(9) "15.55====" 289string(9) "==15.55==" 290string(10) "15.55 " 291string(10) "15.55=====" 292string(10) "=====15.55" 293string(10) "15.55=====" 294string(10) "==15.55===" 295string(16) "15.55 " 296string(16) "15.55===========" 297string(16) "===========15.55" 298string(16) "15.55===========" 299string(16) "=====15.55======" 300string(4) "2990" 301string(4) "2990" 302string(4) "2990" 303string(4) "2990" 304string(4) "2990" 305string(4) "2990" 306string(4) "2990" 307string(4) "2990" 308string(4) "2990" 309string(4) "2990" 310string(4) "2990" 311string(4) "2990" 312string(4) "2990" 313string(4) "2990" 314string(4) "2990" 315string(9) "2990 " 316string(9) "2990=====" 317string(9) "=====2990" 318string(9) "2990=====" 319string(9) "==2990===" 320string(10) "2990 " 321string(10) "2990======" 322string(10) "======2990" 323string(10) "2990======" 324string(10) "===2990===" 325string(16) "2990 " 326string(16) "2990============" 327string(16) "============2990" 328string(16) "2990============" 329string(16) "======2990======" 330 331#### variation with pad string #### 332string(16) "variation=======" 333string(16) "=======variation" 334string(16) "variation=======" 335string(16) "===variation====" 336string(16) "variation1111111" 337string(16) "1111111variation" 338string(16) "variation1111111" 339string(16) "111variation1111" 340string(16) "variation1111111" 341string(16) "1111111variation" 342string(16) "variation1111111" 343string(16) "111variation1111" 344string(16) "variationstring_" 345string(16) "string_variation" 346string(16) "variationstring_" 347string(16) "strvariationstri" 348string(16) "variation1.51.51" 349string(16) "1.51.51variation" 350string(16) "variation1.51.51" 351string(16) "1.5variation1.51" 352string(16) "variation " 353string(16) " variation" 354string(16) "variation " 355string(16) " variation " 356string(16) "variation\t\t\t\" 357string(16) "\t\t\t\variation" 358string(16) "variation\t\t\t\" 359string(16) "\t\variation\t\t" 360 361#### error conditions #### 362Warning: str_pad() expects at least 2 parameters, 0 given in %s on line %d 363 364Warning: str_pad() expects at least 2 parameters, 1 given in %s on line %d 365 366Notice: Undefined variable: input_tring in %s on line %d 367 368Warning: str_pad() expects at most 4 parameters, 5 given in %s on line %d 369 370--- padding string as null --- 371Warning: str_pad(): Padding string cannot be empty in %s on line %d 372 373Warning: str_pad(): Padding string cannot be empty in %s on line %d 374 375Warning: str_pad(): Padding type has to be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH in %s on line %d 376NULL 377Done 378