I have a field where I am rewriting the output, trimming it and providing a read more link. The read more link contains a # because it's a page anchor that triggers a views accordion on the destination page.

Views is converting the # character into %23 in the actual displayed link, which is breaking the link.

How can I convert it back to a # character in the url?


3 Answers 3

function drupal_urlencode($text) {
  if (variable_get('clean_url', '0')) {
    return str_replace(array('%2F', '%3F', '%3D', '%26', '%2523'),
                       array('/', '?', '=', '&', '#'),
  else {
    return str_replace('%2F', '/', urlencode($text));

So this was what we actually ended up doing, no preprocess or overrides required. Thanks to Peter Bull at Acquia for the help on this:

We handled this with some additional Views fields to rewrite the HTML.

  1. Added an additional field for the term description, identical to how you I currently had it, except select "Exclude from display" and remove the "more link".

  2. Changed the order of fields so the new term description field is loaded before the existing one.

  3. Edited the original term description field. Check "Rewrite the output of this field" and put the token for the new field in the textarea, and then append the HTML for your link. So you are rewriting the HTML output as:

[token-for-new-term-description-field] <a href="/mypagewiththeview#training-block-[counter]" class="foo">more</a>

Then I removed the more link as it was included in the rewrite above.

This workaround will preserve the trimmed output of the description field, and allow you to render the link with the appropriate counter ID in the anchor, without running that path through l() and check_plain().

Also needed to prepend the link with a / so it's root-relative. Since it's not being passed though l() it won't do that automatically and if this view page has a path that's not relative to the docroot you could get broken links.


$url = l(t("title of your page"),"$link_url",array('fragment' => 'hash url without #'));

first argument is title of your page

second argument is $link_url means ex. www.google.com/link_url.

and last is array of fragment in this array you can pass link you want to pass after #

ex.http://domain.com/india#rajkot so you haven,t need to use encode or decode any any other conversion

full syntax is

l(t('My Anchor Link'), '', array(
 'fragment' => ' ',
 'external' => TRUE,
 'attributes' => array(
 'title' => 'Title here.'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.