Jump to content

Recommended Posts

Great alliteration in the title. *Ahem*

 

Anyway I have this code that I use for my chat system, And I'm making a command system. However when I send the command, It picks it up as a command etc. but it is so stubborn it will not realise it. I'm guessing it's my human error again like usual but take a look, See if you can help me :( (Please note the filea() function is for debugging it's telling me where the code (by printing to file) is taking me as the way I'm using it I can't just use echo or print whatever and see the results on a page)

 

	if(substr($text, 0, 2)=="::"){ //the command trigger is ::
                    filea("Picked up the start of the command");
		$subt1=substr($text, 2); //seperate the command trigger, In my case :: to execute the command.
                        filea("Just checking the string... ".$subt1);
		$comms=array('ban', 'mute', 'cls', 'sysm', 'unmute');
		$match=false;
		foreach($comms as $comm){
			$commtest2=strpos($subt1, $comm);
			if($comtest2!=false){ //I also tried ==true first with no success
				$match=true;
				$command=$comm;
				break;
			}
		}
                        filea("Ran command checker");
		if($match==false){
                                 filea("Command not found match=".$match);
			exit; //Exit out if there were no matching commands.
		}

 

When I run this command: ::mute test this is the log that gets given to me, As you can see from the code this is what I put in there.

 

3:07 AM) Logged in //this isn't in the code...
(3:07 AM) Set text
(3:07 AM) finding stats of person gergy008
(3:07 AM) Not muted. Continue...
(3:07 AM) Has powers //up to here. But you don't need this. 
(3:07 AM) Picked up the start of the command
(3:07 AM) Just checking the string... mute test
(3:07 AM) Ran command checker
(3:07 AM) Command not found match=

 

As you can see I'm like -.- it's finding that the command is mute but it won't find mute in that string. It's dumb.

 

Can someone help me find what the heck is wrong with this? I will be truely greatful :)

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/222554-strpos-stubbornness/
Share on other sites

If you read the php.net documentation for strpos, you would learn why it is returning a zero(false) value when it finds the matching string starting in position zero and what logic test you would need to use to detect that vs an actual false value due to not finding the string.

 

Link to comment
https://forums.phpfreaks.com/topic/222554-strpos-stubbornness/#findComment-1151001
Share on other sites

I happened to notice a spelling mistake of yours.

 

this:

$commtest2=strpos($subt1, $comm);
if($comtest2!=false){

 

should probably be:

$commtest2=strpos($subt1, $comm);
if($commtest2 != false){

 

was missing an m in $commtest2

Link to comment
https://forums.phpfreaks.com/topic/222554-strpos-stubbornness/#findComment-1151018
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.