字符串检测

发布于 2024-11-26 03:27:50 字数 193 浏览 2 评论 0原文

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

或者有更有效的方法吗?

本质上,我正在努力完成这样的事情。

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

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绮烟 2024-12-03 03:27:50

我相信,这就是您正在寻找的:

<?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,只需更改解析字符的长度即可。

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.

遥远的绿洲 2024-12-03 03:27:50

我认为使用 preg_match 解析命令比编写命令要容易得多简单解析器:

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

这是不区分大小写的,所以 !SAY 也可以工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文