drupal_urlencode

Versions
5 – 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.
  • This function should only be used on paths, not on query string arguments, otherwise unwanted double encoding will occur.

Parameters

$text String to encode

Related topics

▾ 4 functions call drupal_urlencode()

apachesolr_failure in contributions/apachesolr/apachesolr.module
Determines Apache Solr's behavior when searching causes an exception (e.g. Solr isn't available.) Depending on the admin settings, possibly redirect to Drupal's core search.
nodewords_basic_canonical_form_validate in contributions/nodewords/nodewords_basic/includes/nodewords_basic.nodewords.tags.inc
nodewords_basic_canonical_form_validate in contributions/nodewords/nodewords_basic/includes/nodewords_basic.nodewords.tags.inc
url in drupal/includes/common.inc
Generates an internal or external URL.

Code

drupal/includes/common.inc, line 2563

<?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));
  }
}
?>

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.