Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * Converts a string containing an array expression into an actual array.
- *
- * @param string
- * @return array
- */
- function string_to_array( $string ) {
- $string = trim( $string );
- return tokens_to_array( array_slice( token_get_all( "<?php $string" ), 2 ) );
- }
- /*
- * Accepts a set of PHP tokens from string_to_array() and returns a single- or multi-dimensional array.
- *
- * @param array
- * @return array
- */
- function tokens_to_array( $tokens ) {
- $array = array();
- $key = NULL;
- for( $i = 0; $i < count( $tokens ); $i++ ) {
- // End of array reached?
- if( $tokens[$i][0] == ')' )
- return $array;
- // This token & token value.
- $t = $tokens[$i][0];
- if( isset( $tokens[$i][1] ) )
- $tval = $tokens[$i][1];
- // Is this token a number or 'string'?
- if( $t == T_LNUMBER || $t == T_CONSTANT_ENCAPSED_STRING ) {
- // If so, find the next and previous tokens that are not spaces...
- $n = 0;
- $p = 0;
- do { $n++; $tnext = $tokens[$i+$n][0]; } while( $tnext == T_WHITESPACE );
- do { $p--; $tprev = $tokens[$i+$p][0]; } while( $tprev == T_WHITESPACE );
- // Is the next token a double arrow?
- if( $tnext == T_DOUBLE_ARROW ) {
- // If so, this is the key in a key=>value pair.
- if( $t == T_LNUMBER ) {
- $key = intval( $tval );
- } elseif( $t == T_CONSTANT_ENCAPSED_STRING ) {
- $key = substr( $tval, 1, strlen( $tval ) - 2);
- }
- // Is the previous token a double arrow?
- } elseif( $tprev == T_DOUBLE_ARROW ) {
- // If so, this is the single value in a key=>value pair. Multiple values would be preceded by T_ARRAY or a comma.
- if( $t == T_LNUMBER ) {
- $value = intval( $tval );
- } elseif( $t == T_CONSTANT_ENCAPSED_STRING ) {
- $value = substr( $tval, 1, strlen( $tval ) - 2);
- }
- array_push( $array, array( $key => $value ) );
- $key = NULL;
- // This token is either a number or 'string', so it must be a single value with no key defined.
- } else {
- if( $t == T_LNUMBER ) {
- $array[] = intval( $tval );
- } elseif( $t == T_CONSTANT_ENCAPSED_STRING ) {
- $array[] = substr( $tval, 1, strlen( $tval ) - 2);
- }
- }
- // Is token a T_ARRAY?
- } elseif( $t == T_ARRAY ) {
- // If so, call this function recursively to get the value of the array.
- $value = tokens_to_array( array_slice( $tokens, $i + 1 ) );
- if( $key ) {
- $array[$key] = $value;
- $key = NULL;
- } else {
- $array[] = array( $value );
- }
- // Skip ahead to the next end-parenthesis.
- $n = 0;
- do { $n++; $tnext = $tokens[$i+$n][0]; } while( $tnext <> ')' );
- $i = $i + $n;
- } // End if.
- } // Next loop.
- } // End function.
- ?>
- <!DOCTYPE html>
- <h1>Enter an array expression below:</h1>
- <?php
- // Default array expression to appear in <textarea> below.
- $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)";
- ?>
- <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
- <code>
- <textarea name="string" cols="80" rows="10" autofocus><?php if( isset( $_POST['string'] ) ) { echo $_POST['string']; } else { echo $example; } ?></textarea>
- </code>
- <br />
- <input type="submit" name="submit" value="Convert String to Array">
- </form>
- <?php
- if( isset( $_POST['string'] ) ) {
- echo '<h2>Results:</h2>';
- $string = $_POST['string'];
- $array = string_to_array( $string );
- var_dump( $array );
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement