the text in the photo is that: PHP: Player->sendPopup("§a§lExample§r\n§bof a popup not centered"); to make it centered i need to add spaces like: PHP: Player->sendPopup("§a§l Example§r\n§bof a popup not centered"); the problem is that the text can change so, i can't decide how many spaces to add. I tryed with strlen() to count the text lenght... example: PHP: $line1 = 'a text that can change';$line2 = 'another text that can change';$len1 = strlen($line1);//22 in this case$len2 = strlen($line2);//28 in this case$spacesToAdd = floor(((max($len1, $len2) - min($len1, $len2))/2));// 28 - 22 = 6 ; 6/2 = 3 ; floor(3) = 3 . so i need to add 3 spaces before the $line1//to add these spaces i'm using a for loop like: $line1 = ' '.$line1; repeated for 3 times (in this case) ... another way? But is not working (not centered text) Can someone help me please? sorry if my english is not good.
Player->sendPopup("§a§l Example"); Player->sendPopup("§a§l§ bof a popup not centered"); You use this code
solved... is working but if the number is odd , /2 with floor ... there is an half space error , sorry for my bad english, idk how to explain it better
This works poorly because it does not consider different lengths in each character. Using str_pad(): PHP: function alighStringCenter(string $string){ $strlen = function_exists("mb_strlen") ? "mb_strlen" : "strlen"; $lines = explode("\n", $string); $maxLength = max(array_map($strlen, $lines)); foreach($lines as &$line){ $line = str_pad($line, $maxLength, " ", STR_PAD_BOTH); } return implode("\n", $lines);} However, str_pad() is not compatible with multibyte. So your lines will be unreasonably biased if you have Unicode characters there. Using str_repeat(): PHP: function alighStringCenter(string $string){ $strlen = function_exists("mb_strlen") ? "mb_strlen" : "strlen"; $lines = explode("\n", $string); $maxLength = max(array_map($strlen, $lines)); foreach($lines as &$line){ $delta = (int) (($maxLength - $strlen($line)) / 2); if($delta > 0) $line = str_repeat(" ", $delta); } } return implode("\n", $lines);} This checks the line of the string with $strlen, which is the function mb_strlen(), or just strlen() if the multibyte extension is not loaded.