note 57723 deleted from function.sprintf by cmb
| From: | cmb@php.net | Date: | Sun, 14 Jul 2019 11:20:26 +0000 |
| Subject: | note 57723 deleted from function.sprintf by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-211565@lists.php.net to get a copy of this message | ||
Note Submitter: tim dot brouckaert dot NOSPAM at gmail dot com
----
If you want to center align some text using the printf or sprintf functions, you can just use the
following:
function center_text($word){
$tot_width = 30;
$symbol = "-";
$middle = round($tot_width/2);
$length_word = strlen($word);
$middle_word = round($length_word / 2);
$last_position = $middle + $middle_word;
$number_of_spaces = $middle - $middle_word;
$result = sprintf("%'{$symbol}{$last_position}s", $word);
for ($i = 0; $i < $number_of_spaces; $i++){
$result .= "$symbol";
}
return $result;
}
$string = "This is some text";
print center_text($string);
off course you can modify the function to use more arguments.