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 !
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) { //... }
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
!== 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.
even tho i personally prefer you to use NBT tags not using names to store data since it can be edited
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);//Arrayif(in_array("OP II", $name)){//value exists.}
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 \nforeach($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