Jump to content

Recommended Posts

Hello! I made a function to split an IRC message into its: prefix, command, arguments and trailing data.

It works, mostly, except if I add a string that I'm exploding into the trailing data, it will attempt to explode that, instead of the first instance.

Like this:

 

 public function parse_message($str) {
$array = explode(' ', $str);
$username = substr($array[0], 1);
$username = explode('!', $username);
$username = isset($username[0]) ? $username[0] : '';
$command = isset($array[1]) ? $array[1] : '';
$args = '';
$count = count($array);
for($i = 2; $i < $count; $i++)
$args .= $array[$i] . ' ';
$args = explode(' :', $args);
$trailing = isset($args[1]) ? $args[1] : '';
$args = isset($args[0]) ? $args[0] : '';
//:orpheus!1844a5bd@23n.np3.hq2haj.IP PRIVMSG #balls :wah
return array('username' => $username, 'command' => $command, 'args' => $args, 'trail' => $trailing);
}
//:orpheus!1844a5bd@23n.np3.hq2haj.IP PRIVMSG #balls :!eval $this->chat("QUIT :Goodbye.");
//"QUIT :Goodbye" makes it only split up to there.

It should get everything after #balls :, which it does, but if I add an extra " :", it will only get the first index of the array.

 

Also, if someone could maybe find a better way to do this, that'd be great :D

Edited by Magestickown

There is a better way to do this, and it's called Regular Expressions. You can learn everything you need to know about this on regular-expressions.info.

Haha yeah... I understand regex, but only the basics.

Either way, I got it working great now :) Thanks anyway.

 

  	 public function parse_message($str) {
           $arguments = explode(' ', $str);
           $count = count($arguments);
           $username = explode('!', substr($arguments[0], 1));
           $username = isset($username[0]) ? $username[0] : '';
           $command = isset($arguments[1]) ? $arguments[1] : '';

           $args = "";
           for($i = 2; $i < $count; $i++)
               $args .= $arguments[$i] . ' ';
           $trailing_data = stripos($str, ' :');
           $trailing_data = trim(substr($str, $trailing_data + 2));
           return array('username' => $username, 'command' => $command, 'trail' => $trailing_data, 'args' => $args);
       }

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.