2019-04-10 19:12:44 +02:00
|
|
|
class Program {
|
|
|
|
|
|
|
|
private string center_text(string $word){
|
|
|
|
int $tot_width = 30;
|
|
|
|
char $symbol = '-';
|
|
|
|
int $middle = (int) round($tot_width/2);
|
2019-04-17 10:08:03 +02:00
|
|
|
int $length_word = strlen($word);
|
2019-04-10 19:12:44 +02:00
|
|
|
int $middle_word = (int) round($length_word / 2);
|
|
|
|
int $last_position = $middle + $middle_word;
|
|
|
|
int $number_of_spaces = $middle - $middle_word;
|
|
|
|
string $result = sprintf("%'{$symbol}{$last_position}s", $word);
|
|
|
|
for(int $i = 0; $i < $number_of_spaces; $i++) {
|
|
|
|
$result += "$symbol";
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2019-05-17 08:40:41 +02:00
|
|
|
public void main() {
|
2019-04-10 19:12:44 +02:00
|
|
|
string $str = 'Example text';
|
|
|
|
print($this->center_text($str));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|