1
0
mirror of https://github.com/php/doc-zh.git synced 2026-03-25 23:52:17 +01:00
Files
archived-doc-zh/reference/array/functions/extract.xml
2012-10-07 09:32:13 +00:00

355 lines
12 KiB
XML
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: ce8212a9d067d3871e97027c6e41f096151ee82e Maintainer: HonestQiao Status: ready -->
<!-- Reviewed: no Maintainer: HonestQiao -->
<refentry xml:id="function.extract" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>extract</refname>
<refpurpose>从数组中将变量导入到当前的符号表</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>int</type><methodname>extract</methodname>
<methodparam><type>array</type><parameter role="reference">var_array</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>extract_type</parameter><initializer>EXTR_OVERWRITE</initializer></methodparam>
<methodparam choice="opt"><type>string</type><parameter>prefix</parameter><initializer>&null;</initializer></methodparam>
</methodsynopsis>
<para>
本函数用来将变量从数组中导入到当前的符号表中。
</para>
<para>
检查每个键名看是否可以作为一个合法的变量名,同时也检查和符号表中已有的变量名的冲突。
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>var_array</parameter></term>
<listitem>
<para>
一个关联数组。此函数会将键名当作变量名,值作为变量的值。
对每个键/值对都会在当前的符号表中建立变量,并受到
<parameter>extract_type</parameter><parameter>prefix</parameter> 参数的影响。
</para>
<para>
必须使用关联数组,数字索引的数组将不会产生结果,除非用了
<constant>EXTR_PREFIX_ALL</constant> 或者 <constant>EXTR_PREFIX_INVALID</constant>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>extract_type</parameter></term>
<listitem>
<para>
对待非法/数字和冲突的键名的方法将根据
<parameter>extract_type</parameter> 参数决定。可以是以下值之一:
<variablelist>
<varlistentry>
<term><constant>EXTR_OVERWRITE</constant></term>
<listitem>
<simpara>
如果有冲突,覆盖已有的变量。
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><constant>EXTR_SKIP</constant></term>
<listitem>
<simpara>
如果有冲突,不覆盖已有的变量。
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><constant>EXTR_PREFIX_SAME</constant></term>
<listitem>
<simpara>如果有冲突,在变量名前加上前缀 <parameter>prefix</parameter>
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><constant>EXTR_PREFIX_ALL</constant></term>
<listitem>
<simpara>
给所有变量名加上前缀
<parameter>prefix</parameter>
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><constant>EXTR_PREFIX_INVALID</constant></term>
<listitem>
<simpara>
仅在非法/数字的变量名前加上前缀 <parameter>prefix</parameter>
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><constant>EXTR_IF_EXISTS</constant></term>
<listitem>
<simpara>
仅在当前符号表中已有同名变量时,覆盖它们的值。其它的都不处理。
举个例子,以下情况非常有用:定义一些有效变量,然后从 <varname>$_REQUEST</varname> 中仅导入这些已定义的变量。
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><constant>EXTR_PREFIX_IF_EXISTS</constant></term>
<listitem>
<simpara>
仅在当前符号表中已有同名变量时,建立附加了前缀的变量名,其它的都不处理。
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><constant>EXTR_REFS</constant></term>
<listitem>
<simpara>
将变量作为引用提取。这有力地表明了导入的变量仍然引用了
<parameter>var_array</parameter> 参数的值。可以单独使用这个标志或者在
<parameter>extract_type</parameter> 中用 OR 与其它任何标志结合使用。
</simpara>
</listitem>
</varlistentry>
</variablelist>
</para>
<para>
如果没有指定 <parameter>extract_type</parameter>,则被假定为 <constant>EXTR_OVERWRITE</constant>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>prefix</parameter></term>
<listitem>
<para>
注意 <parameter>prefix</parameter> 仅在
<parameter>extract_type</parameter> 的值是
<constant>EXTR_PREFIX_SAME</constant><constant>EXTR_PREFIX_ALL</constant><constant>EXTR_PREFIX_INVALID</constant>
<constant>EXTR_PREFIX_IF_EXISTS</constant>
时需要。
如果附加了前缀后的结果不是合法的变量名,将不会导入到符号表中。前缀和数组键名之间会自动加上一个下划线。
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
返回成功导入到符号表中的变量数目。
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<para>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>4.3.0</entry>
<entry>
增加了 <constant>EXTR_REFS</constant>
</entry>
</row>
<row>
<entry>4.2.0</entry>
<entry>
引进 <constant>EXTR_IF_EXISTS</constant><constant>EXTR_PREFIX_IF_EXISTS</constant>
</entry>
</row>
<row>
<entry>4.0.5</entry>
<entry>
函数可以返回被提取的变量数目。
增加了 <constant>EXTR_PREFIX_INVALID</constant>
从这时起 <constant>EXTR_PREFIX_ALL</constant> 也包括了对数字索引的处理。
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>extract</function> 例子</title>
<para>
<function>extract</function> 的一种可能用法是将 <function>wddx_deserialize</function>
返回的结合数组中的内容导入到符号表变量中去。
</para>
<programlisting role="php">
<![CDATA[
<?php
/* 假定 $var_array 是 wddx_deserialize 返回的数组*/
$size = "large";
$var_array = array("color" => "blue",
"size" => "medium",
"shape" => "sphere");
extract($var_array, EXTR_PREFIX_SAME, "wddx");
echo "$color, $size, $shape, $wddx_size\n";
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
blue, large, sphere, medium
]]>
</screen>
<para>
<varname>$size</varname> 没有被覆盖,因为指定了
<constant>EXTR_PREFIX_SAME</constant>,这使得 <varname>$wddx_size</varname>
被建立。如果指定了 <constant>EXTR_SKIP</constant>,则 <varname>$wddx_size</varname>
也不会被建立。<constant>EXTR_OVERWRITE</constant> 将使 <varname>$size</varname>
的值为“medium”<constant>EXTR_PREFIX_ALL</constant> 将建立新变量
<varname>$wddx_color</varname><varname>$wddx_size</varname>
<varname>$wddx_shape</varname>
</para>
</example>
</para>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
<warning>
<para>
不要对不能信任的数据使用
<function>extract</function>,例如用户的输入(<varname>$_GET</varname> <varname>$_FILES</varname>...)。如果这样做,举例说,要临时运行依赖于
<link linkend="security.globals">register_globals</link>
的老代码,要确保使用不会覆盖的
<parameter>extract_type</parameter> 值,例如
<constant>EXTR_SKIP</constant>,并且要留意应该按照 <link linkend="ini.variables-order">variables_order</link><link linkend="ini">&php.ini;</link>
定义的顺序来提取。
</para>
</warning>
<note>
<para>
If you
have <link linkend="security.globals">register_globals</link>
turned on and you use <function>extract</function>
on <varname>$_FILES</varname> and
specify <constant>EXTR_SKIP</constant>, you may be surprised at
the results.
</para>
<warning>
<para>
This is not recommended practice and is only documented here for
completeness. The use
of <link linkend="security.globals">register_globals</link> is
deprecated and calling <function>extract</function> on untrusted
data such as <varname>$_FILES</varname> is, as noted above, a
potential security risk. If you encounter this issue, it means
that you are using at least two poor coding practices.
</para>
</warning>
<programlisting role="php">
<![CDATA[
<?php
/* Suppose that $testfile is the name of a file upload input
and that register_globals is turned on. */
var_dump($testfile);
extract($_FILES, EXTR_SKIP);
var_dump($testfile);
var_dump($testfile['tmp_name']);
?>
]]>
</programlisting>
<simpara>
You might expect to see something like the following:
</simpara>
<screen>
<![CDATA[
string(14) "/tmp/phpgCCPX8"
array(5) {
["name"]=>
string(10) "somefile.txt"
["type"]=>
string(24) "application/octet-stream"
["tmp_name"]=>
string(14) "/tmp/phpgCCPX8"
["error"]=>
int(0)
["size"]=>
int(4208)
}
string(14) "/tmp/phpgCCPX8"
]]>
</screen>
<simpara>
However, you would instead see something like this:
</simpara>
<screen>
<![CDATA[
string(14) "/tmp/phpgCCPX8"
string(14) "/tmp/phpgCCPX8"
string(1) "/"
]]>
</screen>
<para>
This is due to the fact that
since <link linkend="security.globals">register_globals</link> is
turned on, <varname>$testfile</varname> already exists in the
global scope when <function>extract</function> is called. And
since <constant>EXTR_SKIP</constant> is
specified, <varname>$testfile</varname> is not overwritten with
the contents of the <constant>$_FILES</constant> array
so <varname>$testfile</varname> remains a string.
Because <link linkend="language.types.string.substr">strings may
be accessed using array syntax</link> and the non-numeric string
<literal>tmp_name</literal> is interpreted
as <literal>0</literal>, PHP
sees <varname>$testfile['tmp_name']</varname>
as <varname>$testfile[0]</varname>.
</para>
</note>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>compact</function></member>
<member><function>list</function></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->