note 57723 added to function.sprintf
| From: | tim dot brouckaert dot NOSPAM at gmail dot com | Date: | Wed, 12 Oct 2005 12:35:33 +0000 |
| Subject: | note 57723 added to function.sprintf | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-96894@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/function.sprintf.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+57723
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57723&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57723&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57723&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57723&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57723&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57723&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57723&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+57723&report=yes
Search -- http://master.php.net/manage/user-notes.php