-3

典型的な質問がありますが、それが可能かどうかわかりません。Producerというフィールドがあるフォームがあります。ユーザーがその単語使用してフィールドに挿入すると、その単語が結果に挿入され、ユーザーがその単語使用せずにフィールドに挿入された場合、その単語結果に挿入されます。例を挙げて説明しましょう。

例 ( andという単語がフィールドにある) を実行すると、次の結果が生成されます。

ABCDEF映画のプロデューサーです。

例 (and という単語フィールドにありません) を実行すると、次の結果が生成されます。

XYZ映画のプロデューサーです。

次のコードがあります。

if(!empty($_POST['Producer'])) {
$description .= ' ' . $_POST["Producer"] . ' is/are the producer(s) of the movie';
}

心当たりのある方は教えてください。

4

4 に答える 4

4

as haystack と asneedle でstrpos呼び出すだけです。戻り値が false の場合、文字列には が含まれていません。$_POST['Producer']andand

これで、戻り値に応じて出力を作成できます。

http://php.net/manual/en/function.strpos.php

于 2012-07-20T16:36:20.223 に答える
2

以下のコードは動作するはずです (テストされていません)。

if(!empty($_POST['Producer'])) {
    $producer = $_POST["Producer"]; // CONSIDER SANITIZING
    $pos = stripos($_POST['Producer'], ' and ');
    list($verb, $pl) = $pos ? array('are', 's') : array('is', '');
    $description .= " $producer $verb the producer$pl of the movie";
}

前述のように、フォーマットされた文字列をどのように使用するかによって、$_POST["Producer"] の着信値をサニタイズすることも検討する必要があります。

于 2012-07-20T16:45:26.130 に答える
2
if(!empty($_POST['Producer']))
{
    if(stripos($_POST['Producer'], ' and ') != false) // ' and ' is found
        $producers = $_POST['Producer'] .' are the producers ';
    else
        $producers = $_POST['Producer'] .' is the producer ';

    $description = $producers .'of the movie';
}

' and '一部の名前には「are」という単語が含まれているため、代わりに(空白を使用して)入れました。その'and'ため、名前が1つしかない場合でもtrueを返します。

于 2012-07-20T16:38:48.263 に答える
0

私はこれをテストしていませんが、これに沿った何かが機能するはずです。

$string = $_POST['Producer'];

//This is the case if the user used and.
$start = strstr($string, 'and');
if($start != null)
{
    $newString = substr($string, 0, $start) . "are" . substr($string, $start+3, strlen($string))
}
于 2012-07-20T16:42:48.293 に答える