uTools-Manuals/docs/php/addslashes.html
2019-04-28 19:00:34 +08:00

116 lines
5.9 KiB
HTML
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.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>使用反斜线引用字符串</title>
</head>
<body class="docs"><div id="layout">
<div id="layout-content"><div id="function.addslashes" class="refentry">
<div class="refnamediv">
<h1 class="refname">addslashes</h1>
<p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">addslashes</span> &mdash; <span class="dc-title">使用反斜线引用字符串</span></p>
</div>
<div class="refsect1 description" id="refsect1-function.addslashes-description">
<h3 class="title">说明</h3>
<div class="methodsynopsis dc-description">
<span class="methodname"><strong>addslashes</strong></span>
( <span class="methodparam"><span class="type">string</span> <code class="parameter">$str</code></span>
) : <span class="type">string</span></div>
<p class="para rdfs-comment">
返回字符串,该字符串为了数据库查询语句等的需要在某些字符前加上了反斜线。这些字符是单引号(<em>&#039;</em>)、双引号(<em>&quot;</em>)、反斜线(<em>\</em>)与
NUL<strong><code>NULL</code></strong> 字符)。
</p>
<p class="para">
一个使用 <span class="function"><strong>addslashes()</strong></span>
的例子是当你要往数据库中输入数据时。
例如,将名字
<em>O&#039;reilly</em> 插入到数据库中,这就需要对其进行转义。
强烈建议使用 DBMS 指定的转义函数
(比如 MySQL 是 <span class="function"><a href="mysqli.real_escape_string.html" class="function">mysqli_real_escape_string()</a></span>PostgreSQL 是 <span class="function"><a href="pg_escape_string.html" class="function">pg_escape_string()</a></span>),但是如果你使用的 DBMS 没有一个转义函数,并且使用 <em>\</em> 来转义特殊字符,你可以使用这个函数。
仅仅是为了获取插入数据库的数据,额外的 <em>\</em> 并不会插入。
当 PHP
指令 <a href="sybase.configuration.html#ini.magic-quotes-sybase" class="link">magic_quotes_sybase</a>
被设置成 <em>on</em> 时,意味着插入
<em>&#039;</em> 时将使用
<em>&#039;</em> 进行转义。
</p>
<p class="para">
PHP 5.4 之前 PHP 指令 <a href="info.configuration.html#ini.magic-quotes-gpc" class="link">
magic_quotes_gpc</a> 默认是 <em>on</em> 实际上所有的 GET、POST 和 COOKIE 数据都用被 <span class="function"><strong>addslashes()</strong></span> 了。 不要对已经被
<a href="info.configuration.html#ini.magic-quotes-gpc" class="link">magic_quotes_gpc</a>
转义过的字符串使用
<span class="function"><strong>addslashes()</strong></span>,因为这样会导致双层转义。 遇到这种情况时可以使用函数
<span class="function"><a href="get_magic_quotes_gpc.html" class="function">get_magic_quotes_gpc()</a></span>
进行检测。
</p>
</div>
<div class="refsect1 parameters" id="refsect1-function.addslashes-parameters">
<h3 class="title">参数</h3>
<p class="para">
<dl>
<dt>
<code class="parameter">str</code></dt>
<dd>
<p class="para">
要转义的字符。
</p>
</dd>
</dl>
</p>
</div>
<div class="refsect1 returnvalues" id="refsect1-function.addslashes-returnvalues">
<h3 class="title">返回值</h3>
<p class="para">
返回转义后的字符。
</p>
</div>
<div class="refsect1 examples" id="refsect1-function.addslashes-examples">
<h3 class="title">范例</h3>
<p class="para">
<div class="example" id="example-5892">
<p><strong>Example #1 一个 <span class="function"><strong>addslashes()</strong></span> 例子</strong></p>
<div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Is&nbsp;your&nbsp;name&nbsp;O'reilly?"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;输出:&nbsp;Is&nbsp;your&nbsp;name&nbsp;O\'reilly?<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">addslashes</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
</div>
</div>
</p>
</div>
<div class="refsect1 seealso" id="refsect1-function.addslashes-seealso">
<h3 class="title">参见</h3>
<p class="para">
<ul class="simplelist">
<li class="member"><span class="function"><a href="stripcslashes.html" class="function" rel="rdfs-seeAlso">stripcslashes()</a> - 反引用一个使用 addcslashes 转义的字符串</span></li>
<li class="member"><span class="function"><a href="stripslashes.html" class="function" rel="rdfs-seeAlso">stripslashes()</a> - 反引用一个引用字符串</span></li>
<li class="member"><span class="function"><a href="addcslashes.html" class="function" rel="rdfs-seeAlso">addcslashes()</a> - 以 C 语言风格使用反斜线转义字符串中的字符</span></li>
<li class="member"><span class="function"><a href="htmlspecialchars.html" class="function" rel="rdfs-seeAlso">htmlspecialchars()</a> - 将特殊字符转换为 HTML 实体</span></li>
<li class="member"><span class="function"><a href="quotemeta.html" class="function" rel="rdfs-seeAlso">quotemeta()</a> - 转义元字符集</span></li>
<li class="member"><span class="function"><a href="get_magic_quotes_gpc.html" class="function" rel="rdfs-seeAlso">get_magic_quotes_gpc()</a> - 获取当前 magic_quotes_gpc 的配置选项设置</span></li>
</ul>
</p>
</div>
</div></div></div></body></html>