所以我正在写一个php IRCbot脚本,我想知道..如果我要实现一个字符串检测系统,我是在谈论一个带字符串比较的无限循环吗?
或者有更有效的方法吗?
基本上,我正在努力完成这样的事情。
<User1> !say Hello!
<phpBot> Hello!
答案 0 :(得分:0)
我相信,这就是你要找的东西:
<?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
}
?>
当然,您可以根据需要添加if / else,只需要更改已解析字符的长度。
答案 1 :(得分:0)
我认为使用preg_match解析命令要比编写简单的解析器容易得多:
$input = "!say hello world";
$args = array();
if(preg_match("/^!say\s+(.*)$/i", $input, $args)) {
echo "Saying: \"", $args[1], "\"\n";
}
这是不区分大小写的!SAY也可以。