2016-04-07 4 views
0

[OK]を、私はPHPでやや動的SMSの自動応答を作成しようとしています。今、私は基本的なものを持っていて、1つの単語入力に反応することができます。ここを参照してください: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(); 
} 

私が達成しようとしていますどのようなことは、誰かが「ねえ、販売のためのその犬です」のようなものではテキスト場合、これは対応してもらうことです。現在のところ、「Dog」または「dog」という単語がテキスト化されている場合にのみ機能します。

私は配列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(); 
} 
+0

を誰かがちょうどよりはるかに多くの単語を含むメッセージをテキスト場合、スクリプトは何をすべき」犬"?メッセージに「dog owl dog」が含まれているとどうなりますか? – Sven

+0

$ result内の文字列を検索する必要があります。現在、switch文で完全一致をテストしています。 $ resultを使って探している単語を見つけるためにそれを変更します - あなたはpreg_matchで正しい軌道にいます。 – Stuart

+0

理想的には、誰かが、犬やフクロウなどの言葉よりも多くの単語を含むメッセージにテキストをつけて、キーワードを選んで適切に応答するとすればよいということです。私は最初にそれを働かせたいので、複数のキーワードについて心配しないでください。 – Sam

答えて

0

これはさまざまな方法があると確信しています。ここに私の提案があります:

まず、ユーザー入力からスペースを削除しないでください。スペースのない長い文字列を1つだけ持っていれば、探している文字列を形成する単語の始まりと終わりのために、誤ったマッチを得ることができます。

代わりに、他の文字を削除して小文字に変換した後に、スペース上の文字列を分割して$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 
} 
+0

私はこれが何をしようとしているのか正確に見ていますが、ブロックに入れるとうまくいかないようです。私はそれを正しく統合していないと確信しています。 – Sam

+0

ええと、私は重要な部分をコードの残りの部分に統合するのではなく、答えに含めようとしました。とにかく、コンセプトがうまくいくと確信しています。あなたがオンラインエディタ(コードパッドなど)でそれを使ってあなたの試みを共有することができたなら、私はおそらく何がうまくいかないかを伝えることができます。 –

+0

私はちょっと試してみることができますが、あなたのコードでは、スイッチケースセクションを完全に削除しますか? – Sam

関連する問題