开发者

String Detection

So I'm writing a php IRCbot script, and I'm wondering.. if I were to implement a string detection system, am I talking an infinite loop with string compare?

Or开发者_如何学运维 is there a more efficient way?

Essentially, I'm trying to accomplish something like this.

<User1> !say Hello!
<phpBot> Hello!


This, i believe, is what you are looking for:

<?php
  //asuming that $sentence is the <User1> input
  $sentence='!say Hello!';

  if (substr($sentence,0,4)=='!say'){ //4 is the length of the string !say
      echo ltrim(substr($sentence,4)); //4 is the length of the string !say
  } 
?>

Of course you can add as many if/else as you need, just need to change the length of the parsed characters.


I think using preg_match to parse the commands is much easier than even write a simple parser:

$input = "!say      hello world";
$args = array();
if(preg_match("/^!say\s+(.*)$/i", $input, $args)) {
    echo "Saying: \"", $args[1], "\"\n";
}

This is case insensitive so !SAY would also work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜