0

I've nearly figured out my sms issue, and have it narrowed down to one small issue that I can't seem to get to work.

Here's what I have:

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 */

So the SMS app now can read a sentence and find the keyword no sweat. Problem is, I also need to have the position of the word in relation to where it is in the keyword array. If I manually change the number to the correct position and send a text containing that keyword it functions flawlessly.

Unfortunately array_search doesn't work because it can't accept a dynamic needle. Is there any way to have the array position auto populate based on what keyword is found? In my code above, you can see (hopefully) what I'm trying to do.

Pooya
  • 6,083
  • 3
  • 23
  • 43
Sam
  • 9
  • 2
  • possible duplicate of : http://stackoverflow.com/questions/18680925/find-the-position-of-a-word-in-a-string and http://stackoverflow.com/questions/11398782/find-the-exact-word-position-in-string – Md. Khairul Hasan Apr 09 '16 at 03:20
  • _“Unfortunately array_search doesn't work because it can't accept a dynamic needle”_ – what’s that supposed to mean? What is a “dynamic needle”? – CBroe Apr 09 '16 at 04:26
  • This: array_search would work in the expression array_search('dog','$keyword) where I specifically state an exact value to search for. In the case where I would want the array_search to look for the keyword that's texted in, in my example '$word', that would make my preferred array_search($word,$keyword), which does not work. The needle in this case would be dynamic, because I'm not telling it a static value to search for since the word to look for would change depending on what is texted in. – Sam Apr 09 '16 at 13:02

1 Answers1

0

Solved issue. It can be done dynamically.

 $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('listing','pigeon_show','owl');
  $word = array_intersect($keyword,$text);
  $key = key($word);

if ($word){
  $word[$key]();}
 else
  {index();}
Sam
  • 9
  • 2