Advertisement
sscovil

PHP String-to-Array Function

Feb 1st, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.60 KB | None | 0 0
  1. <?php
  2.    
  3.     /*
  4.     * Converts a string containing an array expression into an actual array.
  5.     *
  6.     * @param string
  7.     * @return array
  8.     */
  9.     function string_to_array( $string ) {
  10.         $string = trim( $string );
  11.         return tokens_to_array( array_slice( token_get_all( "<?php $string" ), 2 ) );
  12.     }
  13.    
  14.    
  15.     /*
  16.     * Accepts a set of PHP tokens from string_to_array() and returns a single- or multi-dimensional array.
  17.     *
  18.     * @param array
  19.     * @return array
  20.     */
  21.     function tokens_to_array( $tokens ) {
  22.         $array = array();
  23.         $key = NULL;       
  24.         for( $i = 0; $i < count( $tokens ); $i++ ) {           
  25.             // End of array reached?
  26.             if( $tokens[$i][0] == ')' )
  27.                 return $array;
  28.            
  29.             // This token & token value.
  30.             $t = $tokens[$i][0];
  31.             if( isset( $tokens[$i][1] ) )
  32.                 $tval = $tokens[$i][1];
  33.            
  34.             // Is this token a number or 'string'?
  35.             if( $t == T_LNUMBER || $t == T_CONSTANT_ENCAPSED_STRING ) {
  36.                 // If so, find the next and previous tokens that are not spaces...
  37.                 $n = 0;
  38.                 $p = 0;
  39.                 do { $n++; $tnext = $tokens[$i+$n][0]; } while( $tnext == T_WHITESPACE );
  40.                 do { $p--; $tprev = $tokens[$i+$p][0]; } while( $tprev == T_WHITESPACE );
  41.                
  42.                 // Is the next token a double arrow?
  43.                 if( $tnext == T_DOUBLE_ARROW ) {
  44.                     // If so, this is the key in a key=>value pair.
  45.                     if( $t == T_LNUMBER ) {
  46.                         $key = intval( $tval );
  47.                     } elseif( $t == T_CONSTANT_ENCAPSED_STRING ) {
  48.                         $key = substr( $tval, 1, strlen( $tval ) - 2);
  49.                     }
  50.                
  51.                 // Is the previous token a double arrow?
  52.                 } elseif( $tprev == T_DOUBLE_ARROW ) {
  53.                     // If so, this is the single value in a key=>value pair. Multiple values would be preceded by T_ARRAY or a comma.
  54.                     if( $t == T_LNUMBER ) {
  55.                         $value = intval( $tval );
  56.                     } elseif( $t == T_CONSTANT_ENCAPSED_STRING ) {
  57.                         $value = substr( $tval, 1, strlen( $tval ) - 2);
  58.                     }
  59.                     array_push( $array, array( $key => $value ) );
  60.                     $key = NULL;
  61.                
  62.                 // This token is either a number or 'string', so it must be a single value with no key defined.
  63.                 } else {
  64.                     if( $t == T_LNUMBER ) {
  65.                         $array[] = intval( $tval );
  66.                     } elseif( $t == T_CONSTANT_ENCAPSED_STRING ) {
  67.                         $array[] = substr( $tval, 1, strlen( $tval ) - 2);
  68.                     }
  69.                 }
  70.            
  71.             // Is token a T_ARRAY?
  72.             } elseif( $t == T_ARRAY ) {
  73.                 // If so, call this function recursively to get the value of the array.
  74.                 $value = tokens_to_array( array_slice( $tokens, $i + 1 ) );
  75.                 if( $key ) {
  76.                     $array[$key] = $value;
  77.                     $key = NULL;
  78.                 } else {
  79.                     $array[] = array( $value );
  80.                 }
  81.                 // Skip ahead to the next end-parenthesis.
  82.                 $n = 0;
  83.                 do { $n++; $tnext = $tokens[$i+$n][0]; } while( $tnext <> ')' );
  84.                 $i = $i + $n;
  85.            
  86.             } // End if.
  87.  
  88.         } // Next loop.
  89.        
  90.     } // End function.
  91.    
  92. ?>
  93.  
  94. <!DOCTYPE html>
  95.  
  96. <h1>Enter an array expression below:</h1>
  97.  
  98. <?php
  99.     // Default array expression to appear in <textarea> below.
  100.     $example = "array(\n  'numbers' => array( 2, 6 ),\n  'value with no key',\n  'another value with no key',\n  'names' => array( 'tom', 'dick', 'harry' ),\n  'keyvals' => array(\n    'key1' => 'val1',\n    'key2' => 'val2',\n  )\n)";
  101. ?>
  102.  
  103. <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  104.     <code>
  105.         <textarea name="string" cols="80" rows="10" autofocus><?php if( isset( $_POST['string'] ) ) { echo $_POST['string']; } else { echo $example; } ?></textarea>
  106.     </code>
  107.     <br />
  108.     <input type="submit" name="submit" value="Convert String to Array">
  109. </form>
  110.  
  111. <?php
  112.     if( isset( $_POST['string'] ) ) {
  113.         echo '<h2>Results:</h2>';
  114.         $string = $_POST['string'];
  115.         $array = string_to_array( $string );
  116.         var_dump( $array );
  117.     }
  118.    
  119. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement