Source of: /manual/en/function.urlencode.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.url.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'function.urlencode.php',
1 => 'urlencode',
),
'up' =>
array (
0 => 'ref.url.php',
1 => 'URL Functions',
),
'prev' =>
array (
0 => 'function.urldecode.php',
1 => 'urldecode',
),
'next' =>
array (
0 => 'refs.remote.other.php',
1 => 'Other Services',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="function.urlencode" class="refentry">
<div class="refnamediv">
<h1 class="refname">urlencode</h1>
<p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">urlencode</span> — <span class="dc-title">URL-encodes string</span></p>
</div>
<a name="function.urlencode.description"></a><div class="refsect1 description">
<h3 class="title">Description</h3>
<div class="methodsynopsis dc-description">
<span class="type">string</span> <span class="methodname"><b>urlencode</b></span>
( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$str</tt></span>
)</div>
<p class="para rdfs-comment">
This function is convenient when encoding a string to be used in a query
part of a URL, as a convenient way to pass variables to the next page.
</p>
</div>
<a name="function.urlencode.parameters"></a><div class="refsect1 parameters">
<h3 class="title">Parameters</h3>
<p class="para">
</p><dl>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">str</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
The string to be encoded.
</p>
</dd>
</dl>
<p>
</p>
</div>
<a name="function.urlencode.returnvalues"></a><div class="refsect1 returnvalues">
<h3 class="title">Return Values</h3>
<p class="para">
Returns a string in which all non-alphanumeric characters except
<i>-_.</i> have been replaced with a percent
(<i>%</i>) sign followed by two hex digits and spaces encoded
as plus (<i>+</i>) signs. It is encoded the same way that the
posted data from a WWW form is encoded, that is the same way as in
<i>application/x-www-form-urlencoded</i> media type. This
differs from the <a href="http://www.faqs.org/rfcs/rfc1738" class="link external">» RFC 1738</a> encoding (see
<a href="function.rawurlencode.php" class="function">rawurlencode()</a>) in that for historical reasons, spaces
are encoded as plus (+) signs.
</p>
</div>
<a name="function.urlencode.examples"></a><div class="refsect1 examples">
<h3 class="title">Examples</h3>
<p class="para">
</p><div class="example">
<p><b>Example #1 <b>urlencode()</b> example</b></p>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">'<a href="mycgi?foo='</span><span style="color: #007700">, </span><span style="color: #0000BB">urlencode</span><span style="color: #007700">(</span><span style="color: #0000BB">$userinput</span><span style="color: #007700">), </span><span style="color: #DD0000">'">'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
<p class="para">
</p><div class="example">
<p><b>Example #2 <b>urlencode()</b> and <a href="function.htmlentities.php" class="function">htmlentities()</a> example</b></p>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br />$query_string </span><span style="color: #007700">= </span><span style="color: #DD0000">'foo=' </span><span style="color: #007700">. </span><span style="color: #0000BB">urlencode</span><span style="color: #007700">(</span><span style="color: #0000BB">$foo</span><span style="color: #007700">) . </span><span style="color: #DD0000">'&bar=' </span><span style="color: #007700">. </span><span style="color: #0000BB">urlencode</span><span style="color: #007700">(</span><span style="color: #0000BB">$bar</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">'<a href="mycgi?' </span><span style="color: #007700">. </span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">(</span><span style="color: #0000BB">$query_string</span><span style="color: #007700">) . </span><span style="color: #DD0000">'">'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
</div>
<a name="function.urlencode.notes"></a><div class="refsect1 notes">
<h3 class="title">Notes</h3>
<blockquote><p><b class="note">Note</b>:
Be careful about variables that may match HTML entities. Things like
&amp, &copy and &pound are parsed by the browser and the
actual entity is used instead of the desired variable name. This is an
obvious hassle that the W3C has been telling people about for years.
The reference is here: <a href="http://www.w3.org/TR/html4/appendix/notes.html#h-B.2.2" class="link external">» http://www.w3.org/TR/html4/appendix/notes.html#h-B.2.2</a>.
<br />
PHP supports changing the argument separator to the W3C-suggested
semi-colon through the arg_separator .ini directive. Unfortunately most
user agents do not send form data in this semi-colon separated format.
A more portable way around this is to use &amp; instead of & as
the separator. You don't need to change PHP's arg_separator for this.
Leave it as &, but simply encode your URLs using
<a href="function.htmlentities.php" class="function">htmlentities()</a> or
<a href="function.htmlspecialchars.php" class="function">htmlspecialchars()</a>.
<br />
</p></blockquote>
</div>
<a name="function.urlencode.seealso"></a><div class="refsect1 seealso">
<h3 class="title">See Also</h3>
<p class="para">
</p><ul class="simplelist">
<li class="member"><a href="function.urldecode.php" class="function" rel="rdfs-seeAlso">urldecode()</a> - Decodes URL-encoded string</li>
<li class="member"><a href="function.htmlentities.php" class="function" rel="rdfs-seeAlso">htmlentities()</a> - Convert all applicable characters to HTML entities</li>
<li class="member"><a href="function.rawurlencode.php" class="function" rel="rdfs-seeAlso">rawurlencode()</a> - URL-encode according to RFC 1738</li>
<li class="member"><a href="function.rawurldecode.php" class="function" rel="rdfs-seeAlso">rawurldecode()</a> - Decode URL-encoded strings</li>
</ul><p>
</p>
</div>
</div><?php manual_footer(); ?>