5

私はPHPを初めて使用し、これに対する適切な答えを見つけることができませんでした。

$whatever = "array('Test','Blah')";
echo $parsed[2]; //This will be "Blah"

$parsedの値を含む$whateverが、文字列ではなく有効な配列として呼び出される変数を作成したいと思います。

次のように、配列の周囲の引用符を削除するだけで配列を作成できることを認識しています。

$whatever = array('Test','Blah');

しかし、私が取り組んでいる実際のコードでは、これは可能ではありません。また、私の実際のコードでは、配列は多次元であるため、文字の置換を伴うものはおそらく実用的ではありませんが、それが最良のオプションであるかどうかを除外するつもりはありません。

要約すると、PHPで文字列を配列として解析するための最良の方法は何ですか?

4

6 に答える 6

12

eval次の関数を使用します:http: //php.net/manual/en/function.eval.php

$whatever = "array('Test','Blah')";
$parsed = eval("return " . $whatever . ";");
echo $parsed[1]; //This will be "Blah"

$whateverどのPHPコードでも実行できるため、変数の内容を確認するように注意してください。

于 2012-08-31T09:44:46.763 に答える
5

より安全な方法(evalなし)は次のとおりです。

$whatever = "array('Test','Blah')";

$search = array("array", "(", ")", "'");
$parsed = explode(',',str_replace($search, '', $whatever));

echo $parsed[1];

これにより、不要なテキストがすべて削除され、コンマ区切り文字で文字列が展開されます。

于 2012-08-31T09:54:07.143 に答える
1

eval()悪です。パフォーマンスが悪く、安全ではありません

したがって、配列がそれほど複雑でない場合は、正規表現を使用してください

$subject = "array('Test','Blah','Blah2','Blah3')";
$pattern = "/'(.*?)'/";
preg_match_all($pattern, $subject, $matches);
echo "<pre>";print_r($matches[1]);
于 2012-08-31T10:01:53.047 に答える
1

これが私が取り組んできたものです。ユニットテストはまだありませんが、かなりうまく機能しているようです。配列構造内からの関数の使用、オブジェクトのインスタンス化、条件などはサポートしていません。私のユースケースではそれらをサポートしたくありません。ただし、必要な機能を自由に追加してください。

/**
 * A class used convert string representations or php arrays to an array without using eval()
 */
class ArrayTokenScanner
{
    /** @var array  */
    protected $arrayKeys = [];

    /**
     * @param string $string   e.g. array('foo' => 123, 'bar' => [0 => 123, 1 => 12345])
     *
     * @return array
     */
    public function scan($string)
    {
        // Remove whitespace and semi colons
        $sanitized = trim($string, " \t\n\r\0\x0B;");
        if(preg_match('/^(\[|array\().*(\]|\))$/', $sanitized)) {
            if($tokens = $this->tokenize("<?php {$sanitized}")) {
                $this->initialize($tokens);
                return $this->parse($tokens);
            }
        }

        // Given array format is invalid
        throw new InvalidArgumentException("Invalid array format.");
    }

    /**
     * @param array $tokens
     */
    protected function initialize(array $tokens)
    {
        $this->arrayKeys = [];
        while($current = current($tokens)) {
            $next = next($tokens);
            if($next[0] === T_DOUBLE_ARROW) {
                $this->arrayKeys[] = $current[1];
            }
        }
    }

    /**
     * @param array $tokens
     * @return array
     */
    protected function parse(array &$tokens)
    {
        $array = [];
        $token = current($tokens);
        if(in_array($token[0], [T_ARRAY, T_BRACKET_OPEN])) {

            // Is array!
            $assoc = false;
            $index = 0;
            $discriminator = ($token[0] === T_ARRAY) ? T_ARRAY_CLOSE : T_BRACKET_CLOSE;
            while($token = $this->until($tokens, $discriminator)) {


                // Skip arrow ( => )
                if(in_array($token[0], [T_DOUBLE_ARROW])) {
                    continue;
                }

                // Reset associative array key
                if($token[0] === T_COMMA_SEPARATOR) {
                    $assoc = false;
                    continue;
                }

                // Look for array keys
                $next = next($tokens);
                prev($tokens);
                if($next[0] === T_DOUBLE_ARROW) {
                    // Is assoc key
                    $assoc = $token[1];
                    if(preg_match('/^-?(0|[1-9][0-9]*)$/', $assoc)) {
                        $index = $assoc = (int) $assoc;
                    }
                    continue;
                }

                // Parse array contents recursively
                if(in_array($token[0], [T_ARRAY, T_BRACKET_OPEN])) {
                    $array[($assoc !== false) ? $assoc : $this->createKey($index)] = $this->parse($tokens);
                    continue;
                }

                // Parse atomic string
                if(in_array($token[0], [T_STRING, T_NUM_STRING, T_CONSTANT_ENCAPSED_STRING])) {
                    $array[($assoc !== false) ? $assoc : $this->createKey($index)] = $this->parseAtomic($token[1]);
                }

                // Parse atomic number
                if(in_array($token[0], [T_LNUMBER, T_DNUMBER])) {

                    // Check if number is negative
                    $prev = prev($tokens);
                    $value = $token[1];
                    if($prev[0] === T_MINUS) {
                        $value = "-{$value}";
                    }
                    next($tokens);

                    $array[($assoc !== false) ? $assoc : $this->createKey($index)] = $this->parseAtomic($value);
                }

                // Increment index unless a associative key is used. In this case we want too reuse the current value.
                if(!is_string($assoc)) {
                    $index++;
                }
            }

            return $array;
        }
    }

    /**
     * @param array $tokens
     * @param int|string $discriminator
     *
     * @return array|false
     */
    protected function until(array &$tokens, $discriminator)
    {
        $next = next($tokens);
        if($next === false or $next[0] === $discriminator) {
            return false;
        }

        return $next;
    }

    protected function createKey(&$index)
    {
        do {
            if(!in_array($index, $this->arrayKeys, true)) {
                return $index;
            }
        } while(++$index);
    }

    /**
     * @param $string
     * @return array|false
     */
    protected function tokenize($string)
    {
        $tokens = token_get_all($string);
        if(is_array($tokens)) {

            // Filter tokens
            $tokens = array_values(array_filter($tokens, [$this, 'accept']));

            // Normalize token format, make syntax characters look like tokens for consistent parsing
            return $this->normalize($tokens);

        }

        return false;
    }

    /**
     * Method used to accept or deny tokens so that we only have to deal with the allowed tokens
     *
     * @param array|string $value    A token or syntax character
     * @return bool
     */
    protected function accept($value)
    {
        if(is_string($value)) {
            // Allowed syntax characters: comma's and brackets.
            return in_array($value, [',', '[', ']', ')', '-']);
        }
        if(!in_array($value[0], [T_ARRAY, T_CONSTANT_ENCAPSED_STRING, T_DOUBLE_ARROW, T_STRING, T_NUM_STRING, T_LNUMBER, T_DNUMBER])) {
            // Token did not match requirement. The token is not listed in the collection above.
            return false;
        }
        // Token is accepted.
        return true;
    }

    /**
     * Normalize tokens so that each allowed syntax character looks like a token for consistent parsing.
     *
     * @param array $tokens
     *
     * @return array
     */
    protected function normalize(array $tokens)
    {
        // Define some constants for consistency. These characters are not "real" tokens.
        defined('T_MINUS')           ?: define('T_MINUS',           '-');
        defined('T_BRACKET_OPEN')    ?: define('T_BRACKET_OPEN',    '[');
        defined('T_BRACKET_CLOSE')   ?: define('T_BRACKET_CLOSE',   ']');
        defined('T_COMMA_SEPARATOR') ?: define('T_COMMA_SEPARATOR', ',');
        defined('T_ARRAY_CLOSE')     ?: define('T_ARRAY_CLOSE',     ')');

        // Normalize the token array
        return array_map( function($token) {

            // If the token is a syntax character ($token[0] will be string) than use the token (= $token[0]) as value (= $token[1]) as well.
            return [
                0 => $token[0],
                1 => (is_string($token[0])) ? $token[0] : $token[1]
            ];

        }, $tokens);
    }

    /**
     * @param $value
     *
     * @return mixed
     */
    protected function parseAtomic($value)
    {
        // If the parameter type is a string than it will be enclosed with quotes
        if(preg_match('/^["\'].*["\']$/', $value)) {
            // is (already) a string
            return $value;
        }

        // Parse integer
        if(preg_match('/^-?(0|[1-9][0-9]*)$/', $value)) {
            return (int) $value;
        }

        // Parse other sorts of numeric values (floats, scientific notation etc)
        if(is_numeric($value)) {
            return  (float) $value;
        }

        // Parse bool
        if(in_array(strtolower($value), ['true', 'false'])) {
            return ($value == 'true') ? true : false;
        }

        // Parse null
        if(strtolower($value) === 'null') {
            return null;
        }

        // Use string for any remaining values.
        // For example, bitsets are not supported. 0x2,1x2 etc
        return $value;
    }
}

使用例:

$tokenScanner = new ArrayTokenScanner();
$array = $tokenScanner->scan('[array("foo" => -123, "foobie" => "5x2", "bar" => \'456\', 111 => 12, "bar", ["null" => null], "bool" => false), 123 => E_ERROR];');
$arrayExport = preg_replace('/[\s\t]+/', ' ', var_export($array, true));
echo stripslashes($arrayExport) . PHP_EOL;
$array2 = $tokenScanner->scan('[array("foo" => 123, "foobie" => "5x2", "bar" => \'456\', 111 => 12, "bar", ["null" => null], "bool" => false), 123 => E_ERROR];');
$arrayExport = preg_replace('/[\s\t]+/', ' ', var_export($array, true));
echo stripslashes($arrayExport);
于 2015-06-14T19:14:09.177 に答える
0

evalがオプションでない場合。配列が常に同じフォーマット(多次元ではない)であるかどうかに応じて、置換関数を使用して、およびすべての引用符を削除array()ます。次に、コンマで文字列を分解します。

これらの配列にこのフォーマットを設定できない方法はありませんか?このように配列を格納することは私には意味がありません。シリアル化またはjsonはより良いオプションです。

于 2012-08-31T09:51:09.737 に答える
0

あなたは書くことができます

$strCreateArray = "$whatever = " . "array('Test','Blah')" . ";";

eval( $strCreateArray );
于 2012-08-31T09:45:11.453 に答える