PHP坚持将字符串与数组进行比较并激活函数

时间:2016-04-07 21:57:43

标签: php arrays function sms response

好的,我正在尝试用PHP创建一个有点动态的短信自动应答器。现在,我有一个基本的启动和运行,可以响应单字输入。见这里:

<?php

/* Include twilio-php, the official Twilio PHP Helper Library,
 * which can be found at
 * http://www.twilio.com/docs/libraries
*/

include('Services/Twilio.php');

/* Controller: Match the keyword with the customized SMS reply. */
function index(){
    $response = new Services_Twilio_Twiml();
    $response->sms("Reply with one of the following keywords:
monkey, dog, pigeon, owl.");
    echo $response;
  }

function monkey(){
    $response = new Services_Twilio_Twiml();
    $response->sms("Monkey. A small to medium-sized primate that
typically has a long tail, most kinds of which live in trees in
tropical countries.");
    echo $response;
}

function dog(){
    $response = new Services_Twilio_Twiml();
    $response->sms("Dog. A domesticated carnivorous mammal that
typically has a long snout, an acute sense of smell, and a barking,
howling, or whining voice.");
    echo $response;
}

function pigeon(){
    $response = new Services_Twilio_Twiml();
    $response->sms("Pigeon. A stout seed- or fruit-eating bird with
a small head, short legs, and a cooing voice, typically having gray and
white plumage.");
    echo $response;
}

function owl(){
    $response = new Services_Twilio_Twiml();
    $response->sms("Owl. A nocturnal bird of prey with large
forward-facing eyes surrounded by facial disks, a hooked beak,
and typically a loud call.");
    echo $response;
}

/* Read the contents of the 'Body' field of the Request. */
$body = $_REQUEST['Body'];

/* Remove formatting from $body until it is just lowercase
 characters without punctuation or spaces. */
$result = preg_replace("/[^A-Za-z0-9]/u", " ", $body);
$result = trim($result);
$result = strtolower($result);

/* Router: Match the ‘Body’ field with index of keywords */
switch ($result) {
   case 'monkey':
        monkey();
        break;
    case 'dog':
        dog();
        break;
    case 'pigeon':
        pigeon();
        break;
    case 'owl':
        owl();
        break;

/* Optional: Add new routing logic above this line. */
    default:
        index();
}

我想要完成的是,如果有人发短信,例如“嘿,那只狗是出售的吗?”,那么就应该做出回应。截至目前,它仅在“狗”或“狗”这个词发短信时才有效。

我一直在搞乱数组,preg_match,stripos / strpos,但是在我的生活中不能让它正常运行。任何人都可以帮助至少指出我正确的方向吗?我正在学习编码以修复我自己的问题,因为没有可用的,所以请耐心等待我这是新手。谢谢!

好的,所以这就是我现在所拥有的,(这不起作用),但我认为是在正确的道路上。这只是交换到上面的代码,而不是整个代码:

/* Read the contents of the 'Body' field of the Request. */
 $body = $_REQUEST['Body'];

 /* Remove formatting from $body until it is just lowercase
characters without punctuation or spaces. */
 $result = rtrim($body, ", . ! ?");
 $result = strtolower($result);
 $result = explode(' ', $result);
 $keyword = array('dog', 'pigeon', 'owl', 'monkey');
 $testword = array_intersect($result, $keyword);
 print($testword);


/* Router: Match the ‘Body’ field with index of keywords */
switch ($testword) {
    case 'monkey':
        monkey();
        break;
    case 'dog':
        dog();
        break;
    case 'pigeon':
        pigeon();
        break;
    case 'owl':
        owl();
        break;

/* Optional: Add new routing logic above this line. */
    default:
        index();
}

1 个答案:

答案 0 :(得分:0)

我确信有很多方法可以做到这一点。这是我的建议:

首先,不要从用户输入中删除空格。如果你只有一个没有空格的长字符串,那么可能会出现错误的匹配,因为这些字符碰巧会形成一个你正在寻找的字符串的单词结尾/开头。

相反,在删除其他字符并转换为小写字母后,将$result转换为字符数组,方法是将字符串拆分为空格。

$result = explode(' ', $result);

然后,使用array_intersect将其与函数名称数组进行比较。

$keywords = array('monkey', 'dog', 'pigeon', 'owl');

$matches = array_intersect($result, $keywords);

任何匹配都对应于您定义的函数,因此您可以像这样调用它们:

foreach ($matches as $match) {
    $match();
    break; // or don't break, depending on what you want to do with multiple matches
}