0
我幾乎找出了我的短信問題,並將其縮小到一個我似乎無法工作的小問題。在數組中找到單詞的位置php
這是我有:
include('Services/Twilio.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);
$text = explode(' ',$result);
$keyword = array('dog','pigeon','owl');
$word = array_intersect($keyword,$text);
$key = array_search($word, array_keys($keyword));
$word[$key]();
/* ^^this is the issue */
所以短信應用程序現在可以讀取一個句子,找到關鍵字無汗。問題是,我還需要將單詞的位置與它在關鍵字數組中的位置相關聯。如果我手動將數字更改爲正確的位置,併發送包含該關鍵字的文本,它的功能完美無瑕。
不幸的是array_search
不起作用,因爲它不能接受動態針。有什麼辦法可以根據找到的關鍵字自動填充數組位置嗎?在我上面的代碼中,你可以看到(希望)我正在嘗試做什麼。
可能重複:http://stackoverflow.com/questions/18680925/find-the-position-of-a-word-in-a-string 和 http://stackoverflow.com/ question/11398782/find-the-exact-word-position-in-string –
_「不幸的是,array_search不起作用,因爲它不能接受動態針頭」_ - 這是什麼意思?什麼是「動態針」? – CBroe
這個:array_search可以在array_search('dog','$ keyword)的表達式中工作,我在這裏明確指定一個確切的值來搜索。在我想要array_search查找發短信的關鍵字的情況下,在我的示例'$ word'中,這會使我首選的array_search($ word,$ keyword)不起作用。在這種情況下針是動態的,因爲我沒有告訴它靜態值來搜索,因爲要查找的單詞將根據發送的內容而改變。 – Sam