-1

晚上好。我正在制作一个 IRC 机器人,当你提到他时会做出回应。我想知道的是,当有人真正说出他的名字时,如何让他回复。这是我到目前为止所拥有的($match[3] 是有人在频道上说的消息,是的,stripos 是因为我希望它不区分大小写):

if (stripos($match[3], "ircBot") !== false) {
    $isMentioned = true;
}else { $isMentioned = false; }

虽然这实际上可以检测是否有人说出了他的名字,但它仅在消息的开头提到他时才有效,例如:

  • "ircBot is at the beginning of this sentence" 将使 $isMentioned 为真
  • “这句话之间有 ircBot”会使 $isMentioned 错误
  • “在这句话的结尾是 ircBot”将使 $isMentioned 为假

如果“ircBot”在 $match[3] 内的任何地方,而不仅仅是开始,我希望它返回 true

4

3 回答 3

1

您必须寻找单词边界以避免有人打电话MircBot

// using in_array
$isMentioned = in_array('ircbot', preg_split('/\s+/', mb_strtolower($match[3])));

// using regex word boundaries
$isMentioned = preg_match('/\b(ircBot)\b/i', $match[3]);

http://3v4l.org/lh3JT

于 2014-11-14T23:27:35.313 回答
0

stristr改为使用

if (stristr($match[3], "ircBot") !== false) {
    $isMentioned = true;
}else { $isMentioned = false; }
于 2014-11-14T23:20:13.527 回答
0

我认为您的错误在其他地方,例如 $match[3] 的构造。这工作正常:

$isMentioned = stripos('This is in the middle of ircBot the string','ircbot') !== false;
echo( $isMentioned ? 'Is Mentioned' : 'Sad ignored bot');
于 2014-11-14T23:31:56.633 回答