<!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>Quote string with slashes in a C style</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="ref.strings.html">String Functions</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.addslashes.html">addslashes</a></div> <div class="up"><a href="ref.strings.html">String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.addcslashes" class="refentry"> <div class="refnamediv"> <h1 class="refname">addcslashes</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">addcslashes</span> — <span class="dc-title">Quote string with slashes in a C style</span></p> </div> <div class="refsect1 description" id="refsect1-function.addcslashes-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><strong>addcslashes</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$str</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$charlist</code></span> )</div> <p class="para rdfs-comment"> Returns a string with backslashes before characters that are listed in <em><code class="parameter">charlist</code></em> parameter. </p> </div> <div class="refsect1 parameters" id="refsect1-function.addcslashes-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">str</code></em></span> <dd> <p class="para"> The string to be escaped. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">charlist</code></em></span> <dd> <p class="para"> A list of characters to be escaped. If <em><code class="parameter">charlist</code></em> contains characters <em>\n</em>, <em>\r</em> etc., they are converted in C-like style, while other non-alphanumeric characters with ASCII codes lower than 32 and higher than 126 converted to octal representation. </p> <p class="para"> When you define a sequence of characters in the charlist argument make sure that you know what characters come between the characters that you set as the start and end of the range. <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">addcslashes</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo[ ]'</span><span style="color: #007700">, </span><span style="color: #DD0000">'A..z'</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// output: \f\o\o\[ \]<br />// All upper and lower-case letters will be escaped<br />// ... but so will the [\]^_`<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> Also, if the first character in a range has a higher ASCII value than the second character in the range, no range will be constructed. Only the start, end and period characters will be escaped. Use the <span class="function"><a href="function.ord.html" class="function">ord()</a></span> function to find the ASCII value for a character. <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">addcslashes</span><span style="color: #007700">(</span><span style="color: #DD0000">"zoo['.']"</span><span style="color: #007700">, </span><span style="color: #DD0000">'z..A'</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// output: \zoo['\.']<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> Be careful if you choose to escape characters 0, a, b, f, n, r, t and v. They will be converted to \0, \a, \b, \f, \n, \r, \t and \v, all of which are predefined escape sequences in C. Many of these sequences are also defined in other C-derived languages, including PHP, meaning that you may not get the desired result if you use the output of <span class="function"><strong>addcslashes()</strong></span> to generate code in those languages with these characters defined in <em><code class="parameter">charlist</code></em>. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.addcslashes-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the escaped string. </p> </div> <div class="refsect1 changelog" id="refsect1-function.addcslashes-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.2.5</td> <td> The escape sequences \v and \f were added. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.addcslashes-examples"> <h3 class="title">Examples</h3> <p class="para"> <em><code class="parameter">charlist</code></em> like "\0..\37", which would escape all characters with ASCII code between 0 and 31. <div class="example" id="example-4816"> <p><strong>Example #1 <span class="function"><strong>addcslashes()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$escaped </span><span style="color: #007700">= </span><span style="color: #0000BB">addcslashes</span><span style="color: #007700">(</span><span style="color: #0000BB">$not_escaped</span><span style="color: #007700">, </span><span style="color: #DD0000">"\0..\37!@\177..\377"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.addcslashes-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.stripcslashes.html" class="function" rel="rdfs-seeAlso">stripcslashes()</a> - Un-quote string quoted with addcslashes</span></li> <li class="member"> <span class="function"><a href="function.stripslashes.html" class="function" rel="rdfs-seeAlso">stripslashes()</a> - Un-quotes a quoted string</span></li> <li class="member"> <span class="function"><a href="function.addslashes.html" class="function" rel="rdfs-seeAlso">addslashes()</a> - Quote string with slashes</span></li> <li class="member"> <span class="function"><a href="function.htmlspecialchars.html" class="function" rel="rdfs-seeAlso">htmlspecialchars()</a> - Convert special characters to HTML entities</span></li> <li class="member"> <span class="function"><a href="function.quotemeta.html" class="function" rel="rdfs-seeAlso">quotemeta()</a> - Quote meta characters</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="ref.strings.html">String Functions</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.addslashes.html">addslashes</a></div> <div class="up"><a href="ref.strings.html">String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>