drupal_urlencode

includes/common.inc, line 2468

Versions
4.7 – 6
drupal_urlencode($text)

Wrapper around urlencode) which avoids Apache quirks.

Should be used when placing arbitrary data in an URL. Note that Drupal paths are urlencoded() when passed through url() and do not require urlencoding() of individual components.

Notes:

  • For esthetic reasons, we do not escape slashes. This also avoids a 'feature' in Apache where it 404s on any path containing '%2F'.
  • mod_rewrite unescapes %-encoded ampersands, hashes, and slashes when clean URLs are used, which are interpreted as delimiters by PHP. These characters are double escaped so PHP will still see the encoded version.
  • With clean URLs, Apache changes '//' to '/', so every second slash is double escaped.

Parameters

$text String to encode

▾ 4 functions call drupal_urlencode()

drupal_query_string_encode in includes/common.inc
Parse an array into a valid urlencoded query string.
theme_comment_post_forbidden in modules/comment/comment.module
Theme a "you can't post comments" notice.
url in includes/common.inc
Generate a URL from a Drupal menu path. Will also pass-through existing URLs.
_update_build_fetch_url in modules/update/update.fetch.inc
Generates the URL to fetch information about project updates.

Code

<?php
function drupal_urlencode($text) {
  if (variable_get('clean_url', '0')) {
    return str_replace(array('%2F', '%26', '%23', '//'),
                       array('/', '%2526', '%2523', '/%252F'),
                       rawurlencode($text));
  }
  else {
    return str_replace('%2F', '/', rawurlencode($text));
  }
}
?>
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.