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

Plugin Help: invalid argument supplied for foreach()

Comments in 'Plugin Development' started by deot, Sep 19, 2015.

Thread Status:
Not open for further replies.
  1. deot
    Offline

    deot Active Member Plugin Developer

    Joined:
    Apr 22, 2015
    Posts:
    219
    Plugins:
    1
    Minecraft User:
    deot
    Im coding a plugin that I will submit to forum later on :)
    But I keep getting an error Invalid Argument Supplied for foreach()

    Does anybody know what's wrong with my code? How can I fix this?
    PHP:
     public function onChat(PlayerChatEvent $e){
            
    $msg $e->getMessage();
                foreach(
    $msg as $message){
                   
    //...//
                
    }
        }
  2. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    You are attempting to use foreach() on a string. It only works on arrays. http://php.net/manual/en/control-structures.foreach.php It would be helpful if you told us what you wanted to make, so that we have an idea on what you want to do and we could give you more tips on it. And a little more PHP knowledge would help. ;)
    Last edited: Sep 19, 2015
    deot likes this.
  3. deot
    Offline

    deot Active Member Plugin Developer

    Joined:
    Apr 22, 2015
    Posts:
    219
    Plugins:
    1
    Minecraft User:
    deot
    Im coding a plugin to block players from using capital letters :)
    Most of the things has been finished, command, permission :)
    Just that foreach part :(

    I have no idea on how to fix that :/ LOL
  4. deot
    Offline

    deot Active Member Plugin Developer

    Joined:
    Apr 22, 2015
    Posts:
    219
    Plugins:
    1
    Minecraft User:
    deot
    So, make it like this?
    PHP:
     $msg = array($e->getMessage());
  5. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
  6. deot
    Offline

    deot Active Member Plugin Developer

    Joined:
    Apr 22, 2015
    Posts:
    219
    Plugins:
    1
    Minecraft User:
    deot
    Yes, I checked but I have no idea on this foreach error :/
    Can you help me?
  7. deot
    Offline

    deot Active Member Plugin Developer

    Joined:
    Apr 22, 2015
    Posts:
    219
    Plugins:
    1
    Minecraft User:
    deot
    Can you give me the fixed code? :) About that foreach error Really thank you for that :)
  8. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    PHP:
    foreach(str_split($message) as $letter){
        
    //do something for each letter in the message
    }
    BTW this thread isn't in the right section, it should be in Plugin Development.
    deot likes this.
  9. deot
    Offline

    deot Active Member Plugin Developer

    Joined:
    Apr 22, 2015
    Posts:
    219
    Plugins:
    1
    Minecraft User:
    deot
    Thank you :)
    I was just looking for help, so the first thing in my head is to "Help" section :p LOL
  10. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Use logic.
    If the message is in full caps, it is the same even after we strtoupper it.
    However, this also detects purely symbolic messages.
    Therefore, we also have to make sure it is different after we strtolower it.
    However, this also detects emotions like :D or short messages like OK.
    Hence, we should check if the text is long (e.g. at least 5 characters long).

    Summary:
    PHP:
    strtolower($text) !== $text and strtoupper($text) === $text and strlen($text) >= 5
    deot and Taha_The_Hacker like this.
  11. deot
    Offline

    deot Active Member Plugin Developer

    Joined:
    Apr 22, 2015
    Posts:
    219
    Plugins:
    1
    Minecraft User:
    deot
    @PEMapModder
    Thanks! :) Hmmm, I will look into that code :) Credit goes to you :)

    If you want to, help me on this repo:
    I use ctype_upper to check, its working, but I don't know how to make it count the uppercase used...
    https://github.com/deotern/CapsLimit
  12. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    No credit to me? I was the one that helped you with caps checking. :( Anyways I made a PR on your GitHub repo, includes minor fixes. :D
    Last edited: Sep 22, 2015
    deot likes this.
  13. deot
    Offline

    deot Active Member Plugin Developer

    Joined:
    Apr 22, 2015
    Posts:
    219
    Plugins:
    1
    Minecraft User:
    deot
    Yeah, credit goes you too! :) Anyway, @Gamecrafter , the way you used to detect Caps still don't work... :( I'm currently using ctype_upper to check uppercase.. but my problem is it can't check the amount of it... LOL :(

    Still waiting for help... I'm hoping I can submit to Forum... LOL...
  14. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    Try strpos() instead of strstr(). I think it will work.
  15. deot
    Offline

    deot Active Member Plugin Developer

    Joined:
    Apr 22, 2015
    Posts:
    219
    Plugins:
    1
    Minecraft User:
    deot
    Thank you! I'm trying it! :) And I merged your PR, really thank you! :)
  16. deot
    Offline

    deot Active Member Plugin Developer

    Joined:
    Apr 22, 2015
    Posts:
    219
    Plugins:
    1
    Minecraft User:
    deot
    @Gamecrafter It's still not working... :( Maybe we need to stick back to my original code, use ctype_upper or maybe that code by @PEMapModder :)
  17. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    Sure...
  18. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Instead of using str_split, I suggest using this instead:
    PHP:
    $text "I have FIVE capital letters.";
    $strlen strlen($text);
    $asciiA ord("A"); // hardcode the real ASCII code to make it run faster
    $asciiZ ord("Z"); // same
    $cnt 0;
    for(
    $i 0$i $strlen$i++){
      
    $char $text[$i];
      
    $ascii ord($char);
      if(
    $asciiA <= $ascii and $ascii <= $asciiZ){
        
    $cnt++;
      }
    }
    $cnt is the number of capital letters, which should be 5 in this case.
    deot and jojoe77777 like this.
  19. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    jojoe77777 likes this.
  20. deot
    Offline

    deot Active Member Plugin Developer

    Joined:
    Apr 22, 2015
    Posts:
    219
    Plugins:
    1
    Minecraft User:
    deot
Thread Status:
Not open for further replies.

Share This Page

Advertisement