1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Get data/string from array

Discussion in 'Development' started by Az928, Feb 27, 2017.

  1. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    Hi, I was wondering how to check if array contents a text.
    Example I set a sword name to:
    PHP:
     $item->setCustomName("TheSword\n OP II");
    Now how to check if sword name contains the array "OP II"?

    Thanks for help !
     
  2. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Nothing in your post is an array. You just need to check if a string contains a specific substring. Try stripos. You would probably end up with something like this:
    PHP:
     //Assuming $item is an instance of Item
     
    $name $item->getCustomName();
     if(
    stripos($name"OP II") !== false) {
      
    //...
      
    }
     
    Last edited: Feb 27, 2017
  3. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    Ah my bad XD , But what will
    PHP:
     if(strips($name"OP II" !== false){ 
    Do? I mean what will "false" do here, Sorry if I sound Nooby XD
     
  4. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    !== false means that the item name contains "OP II", as desired. So the code inside this conditional statement is only run when the item name does contain the string.
     
  5. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    even tho i personally prefer you to use NBT tags not using names to store data since it can be edited
     
    corytortoise likes this.
  6. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    Example on how to use NBT? Never used NBT before except when spawning mobs...
     
  7. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    check the named tag
     
  8. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Wrong. It should be...
    PHP:
     if(strpos($name"OP II") !== false){ 
    I think you wanted to convert the string to an array and check whether "OP II" exists.
    So...
    PHP:
    $item->setCustomName("TheSword\nOP II");

    $name $item->getName();//"TheSword\nOP II"
    $name explode("\n"$name);//Array
    if(in_array("OP II"$name)){
    //value exists.
    }
     
  9. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    YESS! That's what I need! Thanks!
     
  10. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    By the way, is there any way to remove string/array from custom name?
     
  11. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    of course:
    PHP:
    /** @var $item pocketmine\item\Item */
    $item->setCustomName("TheSword\nOP II");

    $name $item->getName();//"TheSword\nOP II"
    $name explode("\n"$name); //'Explode' the string into an array using the delimiter \n
    foreach($name as $index => $namepart){
      if(
    $name == "OP II"){ //found OP II in name
        //remove:
        
    unset($name[$index]);
      }
    }
    //rewrite the array to string
    $item->setCustomName(implode($name"\n")); //'Implode' the array into an string using \n as an delimiter
     
  12. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
  13. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    This should be in the facepalm section.
     
    Sandertv likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.