2011-07-24 134 views
1

所以我正在写一个php IRCbot脚本,我想知道..如果我要实现一个字符串检测系统,我说的字符串比较无限循环?字符串检测

或者是否有更有效的方法?

本质上,我试图完成这样的事情。

<User1> !say Hello! 
<phpBot> Hello! 
+0

为什么无限循环?为什么不简单地检查每个输入的字符串? – Dor

回答

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你需要,只需要改变解析字符的长度添加尽可能多。

0

我认为使用preg_match解析命令要容易得多,甚至比写一个简单的解析器:

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

这是不区分大小写这么说也将工作。