Menu

Capturing all arguments as 1 string

Help
2003-11-01
2004-01-12
  • Nobody/Anonymous

    I'm trying to make a function to let the bot say something.

    !speak adminuser adminpass I AM A MACHINE

    Then I want the bot to say: I AM A MACHINE

    But how can I capture all words after the second argument as a single string? Normally you use $data->messageex[1];

    But I want $data->messageex[3]; to be the whole sentence: I AM A MACHINE

     
    • Nobody/Anonymous

      I think this might work:

      $msg = $data->messageex;
      $msg[0] = '';
      $msg[1] = '';
      $msg[2] = '';
      $message = substr( implode( ' ', $msg ), 3 );

      In words, I clear out the '!speak', 'adminuser' and 'adminpass' parts, then implode the array to a string, with each part seperated by a space. The first three characters of this string would be spaces (because there are three empty parts), which should be removed by the substr function.

      $message should now be 'I AM A MACHINE'

       
    • Wild Bill

      Wild Bill - 2004-01-12

      That would work. You might also do it this way:

      $args = preg_quote("$data->messagex[0] $data->messagex[1] $data->messagex[2]");
      $message = ereg_replace("^$args ", '' implode(' ', $data->messageex));

       

Log in to post a comment.