php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #62834 Allow str_pad() to break 'both' tie on the left
Submitted: 2012-08-16 05:39 UTC Modified: 2017-09-12 10:30 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: gphemsley at gmail dot com Assigned:
Status: Open Package: Strings related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2012-08-16 05:39 UTC] gphemsley at gmail dot com
Description:
------------
When using STR_PAD_BOTH with str_pad(), if there is a tie (i.e. an even/odd length mismatch), the tie will be broken using the default value: adding the string to the right.

There should be a way to force str_pad() to break the tie to left, so that the left side has one more of the string than the right side.

Perhaps add STR_PAD_BOTH_LEFT=3 and alias STR_PAD_BOTH=2 to STR_PAD_BOTH_RIGHT=2.

Test script:
---------------
<?php

define( 'STR_PAD_BOTH_RIGHT', STR_PAD_BOTH ); // 2
define( 'STR_PAD_BOTH_LEFT', 3 );

echo str_pad( '2', 4, ' ', STR_PAD_RIGHT ) . "\n";      // '2   '
echo str_pad( '2', 4, ' ', STR_PAD_LEFT ) . "\n";       // '   2'
echo str_pad( '2', 4, ' ', STR_PAD_BOTH ) . "\n";       // ' 2  '
echo str_pad( '2', 4, ' ', STR_PAD_BOTH_RIGHT ) . "\n"; // ' 2  '
echo str_pad( '2', 4, ' ', STR_PAD_BOTH_LEFT ) . "\n";  // '  2 '

?>

Expected result:
----------------
2   
   2
 2  
 2  
  2 

Actual result:
--------------
2   
   2
 2  
 2  

Warning: str_pad(): Padding type has to be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH in str_pad.php on line 10



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-09-12 10:30 UTC] cmb@php.net
-Package: Unknown/Other Function +Package: Strings related
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Dec 04 06:01:31 2024 UTC