Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Solved Centered Popups

Comments in 'Plugin Development' started by Svile, Feb 9, 2016.

  1. Svile
    Offline

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
    [​IMG]

    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.
    Last edited: Feb 9, 2016
  2. Svile
    Offline

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
    I meant the alignment

    [​IMG]
  3. Svile
    Offline

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
    no one can help?
  4. minebuilder0110
    Offline

    minebuilder0110 Notable Member

    Joined:
    Dec 8, 2014
    Posts:
    485
    Minecraft User:
    Apparitional
    I personally think calculating the spaces depending on the strlen is the only way...
  5. Svile
    Offline

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
    and i think i'm doing it in the right way
  6. PolskiTHEPLAYER
    Offline

    PolskiTHEPLAYER New Member

    Joined:
    Apr 1, 2015
    Posts:
    55
    Minecraft User:
    Flsmers
    Player->sendPopup("§a§l Example");
    Player->sendPopup("§a§l§ bof a popup not centered");

    You use this code
  7. CookieSteve
    Offline

    CookieSteve Active Member

    Joined:
    Apr 22, 2014
    Posts:
    132
    Minecraft User:
    iStrqfeChris
    He wants 2 lines and that would just override the previous popup
  8. Svile
    Offline

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
    o_O
  9. PolskiTHEPLAYER
    Offline

    PolskiTHEPLAYER New Member

    Joined:
    Apr 1, 2015
    Posts:
    55
    Minecraft User:
    Flsmers
    2 lines is add space in message
  10. Svile
    Offline

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
    solved... is working :D 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
  11. Svile
    Offline

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
    -_-' pls stop replying if you don't understand what i need btw , i solved !
  12. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    PocketKiller, Svile and applqpak like this.
  13. Svile
    Offline

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
  14. CaptainKenji17
    Offline

    CaptainKenji17 Active Member

    Joined:
    Apr 22, 2014
    Posts:
    252
    Minecraft User:
    CaptainKenji17
    sendTip();
  15. Svile
    Offline

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
    [​IMG]
  16. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    So... Sorry for bumping, but what was your solution?
  17. Vaivez66
    Offline

    Vaivez66 Notable Member Plugin Developer

    Joined:
    Jun 10, 2015
    Posts:
    402
    Plugins:
    2
  18. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    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.

Share This Page

Advertisement