Skip to content

Instantly share code, notes, and snippets.

@xxxkurosukexxx
Last active August 29, 2015 14:14
Show Gist options
  • Save xxxkurosukexxx/a0b1efe2c7f31774ccb8 to your computer and use it in GitHub Desktop.
Save xxxkurosukexxx/a0b1efe2c7f31774ccb8 to your computer and use it in GitHub Desktop.
[php] centeringText
<?php
// config -----
$strArr = array(
'requestInfo',
'memGet',
'memGetInfo',
'apiInfo',
'memSetInfo',
'resInfo',
'012345678901234567890123456789012345678901234567890123456789',
);
$totalLength = 50;
$padChar = '*';
// ----- config
foreach ($strArr as $txt) {
var_dump(makeCenteringText($txt, $totalLength, $padChar));
}
function makeCenteringText($txt, $totalLength, $padChar) {
// deny overflow total length? uncomment this.
//if (strlen((string)$txt) > $totalLength) { return null; }
$padLengthPre = round((int)$totalLength/2, 0, PHP_ROUND_HALF_DOWN) + round(strlen((string)$txt)/2, 0, PHP_ROUND_HALF_UP);
return sprintf('%\''.(string)$padChar.'-'.(int)$totalLength.'s', sprintf('%\''.(string)$padChar.(int)$padLengthPre.'s', (string)$txt));
}
// output.
//
// string(50) "********************requestInfo*******************"
// string(50) "**********************memGet**********************"
// string(50) "********************memGetInfo********************"
// string(50) "**********************apiInfo*********************"
// string(50) "********************memSetInfo********************"
// string(50) "**********************resInfo*********************"
// string(60) "012345678901234567890123456789012345678901234567890123456789"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment