Toolbox::str_pad PHP Méthode

str_pad() static public méthode

Replace str_pad() who bug with utf8
static public str_pad ( $input, $pad_length, $pad_string = " ", $pad_type = STR_PAD_RIGHT ) : string
$input string input string
$pad_length integer padding length
$pad_string string padding string (default '')
$pad_type integer padding type (default STR_PAD_RIGHT)
Résultat string
    static function str_pad($input, $pad_length, $pad_string = " ", $pad_type = STR_PAD_RIGHT)
    {
        $diff = strlen($input) - self::strlen($input);
        return str_pad($input, $pad_length + $diff, $pad_string, $pad_type);
    }

Usage Example

 /**
  * Format XML, ie indent it for pretty printing
  *
  * @param $xml simplexml object
  * @return string
  **/
 static function formatXML($xml)
 {
     $string = str_replace("><", ">\n<", $xml->asXML());
     $token = strtok($string, "\n");
     $result = '';
     $pad = 0;
     $matches = array();
     $indent = 0;
     while ($token !== FALSE) {
         // 1. open and closing tags on same line - no change
         if (preg_match('/.+<\\/\\w[^>]*>$/', $token, $matches)) {
             $indent = 0;
             // 2. closing tag - outdent now
         } else {
             if (preg_match('/^<\\/\\w/', $token, $matches)) {
                 $pad = $pad - 3;
                 // 3. opening tag - don't pad this one, only subsequent tags
             } else {
                 if (preg_match('/^<\\w[^>]*[^\\/]>.*$/', $token, $matches)) {
                     $indent = 3;
                 } else {
                     $indent = 0;
                 }
             }
         }
         $line = Toolbox::str_pad($token, strlen($token) + $pad, '  ', STR_PAD_LEFT);
         $result .= $line . "\n";
         $token = strtok("\n");
         $pad += $indent;
         $indent = 0;
     }
     return $result;
 }
All Usage Examples Of Toolbox::str_pad