PHP如何比较arrays和activate函数
好的,我正在尝试用PHP创建一个动态的sms自动响应程序。现在,我有一个基本的可以响应单个单词输入的程序。请看这里:PHP如何比较arrays和activate函数,php,arrays,function,sms,response,Php,Arrays,Function,Sms,Response,好的,我正在尝试用PHP创建一个动态的sms自动响应程序。现在,我有一个基本的可以响应单个单词输入的程序。请看这里: <?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: M
<?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();
}
array_intersect返回一个数组,看起来您将“testword”作为字符串。也许你可以尝试在数组中检查它与哪个关键字完全匹配Ok,那么使用“in_array”函数,我将如何执行以下操作?:PHP应用程序需要检查字符串中的关键字“猫头鹰、鸽子、猴子、狗”,例如“那只狗花了多少钱?”或“你能告诉我关在笼子里的猴子的情况吗?”?,如果找到关键字匹配,则基于关键字运行函数。
/* 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();
}