0

みんなphpでオペランド入力を検証するにはどうすればよいですか?非常に単純な計算機ページを作成しています。したがって、「+」「-」および「。」数値以外の有効な入力です。したがって、is_numericは十分な検証ではありません。また、drupalでこの検証を実装する方法を知っている場合は、遠慮なく投稿してください。ちなみにdrupalを使っています。ちなみに、これが私のDrupalコードです。簡単な計算機モジュールを作成しました。

<?php
/**
 * Implements hook_menu()
  */

function calculator_menu(){
    $items['calculator-page'] = array(
    'page callback' => 'drupal_get_form',
    'page arguments' => array('calculator_form'),
    'access callback' => TRUE,

    );
    return $items;
    }



function calculator_form(){

    $form['firstoperand'] = array(
    '#title' => t('First operand'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#rules' => 'numeric'
    );

    $form['operator'] = array(
    '#type' => 'select',
    '#options' => array(
        '+' => t('Plus'),
        '-' => t('Minus'),
        '*' => t('Times'),
        '/' => t('Divided by'),
    ),
    '#required' => TRUE,
    );


    $form['secondoperand'] = array(
    '#title' => t('Second operand'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#rules' => 'numeric'
    );

    $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Generate',
    );

    $form['#submit'][] = 'calculator_form_submit';

    return $form;


    }

function calculator_form_submit($form, &$form_state){

    $firstoperand=$form_state['values']['firstoperand'];
    $operator=$form_state['values']['operator'];
    $secondoperand=$form_state['values']['secondoperand'];

    if(!is_numeric($firstoperand) || !is_numeric($secondoperand)){
        drupal_set_message("Must use numbers");
    }
    else{

    /*if($operator=='+'){

    $result= $firstoperand+$secondoperand;
    }
    if($operator=='-'){

    $result= $firstoperand-$secondoperand;
    }
    if($operator=='*'){

    $result= $firstoperand*$secondoperand;
    }
    if($operator=='/'){

    $result= $firstoperand/$secondoperand;
    }*/

    $result = $firstoperand+$operator+$secondoperand;




    drupal_set_message($result);

    }


    }
?>
4

2 に答える 2

2

preg_matchを使用して、これらのシンボルに一致する正規表現パターンを見つけることを検討します。例えば:

  1 <?php
  2 $subject = "1.00+2x3/4";
  3 $pattern = '/\.|\+|x|\//';
  4 preg_match_all($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
  5 print_r($matches);
  6 ?>

これにより、次のようになります。

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => .
                    [1] => 1
                )

            [1] => Array
                (
                    [0] => +
                    [1] => 4
                )

            [2] => Array
                (
                    [0] => x
                    [1] => 6
                )

            [3] => Array
                (
                    [0] => /
                    [1] => 8
                )

        )

)
于 2013-03-15T15:32:58.470 に答える
0

Drupal Form Apiモジュールを使用して、正規表現ルールを利用できます。

$form['firstoperand'] = array(
    '#rules' => 'regexp[/^((\+|\-)?[1-9]\d*(\.\d+)?)|((\+|\-)?0?\.\d+)$/]'
    '#filters' => 'trim'
    );
于 2013-03-15T15:57:39.260 に答える