[ create a new paste ] login | about

Link: http://codepad.org/hpAof4kV    [ raw code | output | fork ]

PHP, pasted on Sep 26:
<?php
    error_reporting(-1);

    Class TestArrayWalk
    {
        /** @var null|array */
        protected $userInput = null;

        /**
         * This expects to be passed an array of the users input from
         * the input fields.
         *
         * @param  array $input
         * @return void
         */
        public function setUserInput( array $input )
        {
            $this->userInput = $input;

            // Lets explode the users input and format it in a way that this class
            // will use for marking
            foreach( $this->userInput as &$rawValue){
                    $rawValue = array(
                        'raw' => $rawValue,
                        'words' => $this->splitIntoKeywordArray( $rawValue ),
                        'marked' => false,
                        'matched' => array()
                    );
            }
        }

        public function getUserInput()
        {
            return $this->userInput;
        }

        protected function splitIntoKeywordArray( $input )
        {
            if ( ! is_string( $input )){ return array(); }
            return preg_split('/(\s|[\.,\/:;!?])/', $input, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
        }

    }


    $testArrayWalk = new TestArrayWalk();
    $testArrayWalk->setUserInput(
            array(
                'This is a test input',
                'This is another test input'
                )
        );

    var_dump( $testArrayWalk->getUserInput() );


Output:
array(2) {
  [0]=>
  array(4) {
    ["raw"]=>
    string(20) "This is a test input"
    ["words"]=>
    array(9) {
      [0]=>
      string(4) "This"
      [1]=>
      string(1) " "
      [2]=>
      string(2) "is"
      [3]=>
      string(1) " "
      [4]=>
      string(1) "a"
      [5]=>
      string(1) " "
      [6]=>
      string(4) "test"
      [7]=>
      string(1) " "
      [8]=>
      string(5) "input"
    }
    ["marked"]=>
    bool(false)
    ["matched"]=>
    array(0) {
    }
  }
  [1]=>
  array(4) {
    ["raw"]=>
    string(26) "This is another test input"
    ["words"]=>
    array(9) {
      [0]=>
      string(4) "This"
      [1]=>
      string(1) " "
      [2]=>
      string(2) "is"
      [3]=>
      string(1) " "
      [4]=>
      string(7) "another"
      [5]=>
      string(1) " "
      [6]=>
      string(4) "test"
      [7]=>
      string(1) " "
      [8]=>
      string(5) "input"
    }
    ["marked"]=>
    bool(false)
    ["matched"]=>
    array(0) {
    }
  }
}


Create a new paste based on this one


Comments: