Hey gang, :-)

Ok, so I'm trying to follow the Drupal way and use the link l() wrapper to do my links. :-)

Basically, I want to set up a link to point to this particular anchored spot on the web page:

http://www.ithou.org/user/2/edit/#edit-picture_delete

However, when I set this up as my link:


l('Update your Picture', "user/$uid/edit/#edit-picture_delete", 
array('title' => t('Update Picture.')), NULL, NULL, FALSE, TRUE);

I get sent to this html address:

http://www.ithou.org/user/2/edit/%23edit-picture_delete

Note the way in which the pound sign # got replaced by %23.

I've looked over some of the APIs... and it looks like drupal_urlencode is doing some kind of substitution?

Anyhow, how do I make it stop substituting in the %23?

Thanks!

Albert
www.ithou.org

Comments

alexmc’s picture

Surely that is correct URL encoding? And they are identical URLs.

PS It isnt a pound sign, it is a hash :-)

skellba’s picture

Hi,

if you look at the code to the function l() in includes/common.inc you will find a 5th parameter which is especially for your problem. So your code should read something like:

l('Update your Picture', "user/$uid/edit/", 
array('title' => t('Update Picture.')), NULL, "edit-picture_delete", FALSE, TRUE);

regards

Stefan

ajwwong’s picture

That looks like it's gonna be perfect! Thanks so much :-)

Albert
Esalen Alumni Group

markhope’s picture

Thanks for this. I've asked about this several times but not had answers.

M

LUTi’s picture

I have Drupal 4.7.4 installed on a Fedora Core 4 machine with Apache server (with all of the latest updates...). As I've noticed some issues with links containing %23 or %2523 (non-working link to a new comment - for anonymous user only, when go indirectly through login screen - in a comment module, link to unsubscribe in a notify module), I've edited a bit the drupal_urlencode($text) function - instead of replacements (line 1322 of common.inc) array('/', '%2526', '%2523') I am using array('/', '&', '#') now, and it seems my problems have disappeared... Everything seems to work just fine, even if not tested deeply until now.

1. Is it OK like that, or which (negative?) consequences can I expect?
2. If it is OK, why it is not like that in Drupal by default?

momper’s picture

hello

i had the same problem - i hope your fix works :)
are we the only ones with this problem? its core ... normally it has to be a big problem for a lot of sites ...

greetings momper

momper’s picture

hello

original:

function drupal_urlencode($text) {
  if (variable_get('clean_url', '0')) {
     return str_replace(array('%2F', '%26', '%23'),
     array('/', '%2526', '%2523'),
     urlencode($text));
  }
  else {
    return str_replace('%2F', '/', urlencode($text));
  }
}

modified fix:
function drupal_urlencode($text) {
  if (variable_get('clean_url', '0')) {
    return str_replace(array('%2F', '%26', '%2523'),
                       array('/', '&', '#'),
                       urlencode($text));
  }
  else {
    return str_replace('%2F', '/', urlencode($text));
  }
}

i had the same problem - i hope the fix works :)
are we the only ones with this problem? its core ... normally it has to be a big problem for a lot of sites ...

greetings momper

KelvinWong’s picture

function drupal_urlencode($text) {
  if (variable_get('clean_url', '0')) {
    return urldecode(str_replace(array('%2F', '%26', '%23'),
                       array('/', '%2526', '%2523'),
                       urlencode($text)));

				  
  }
  else {
    return urldecode(str_replace('%2F', '/', urlencode($text)));
  }
}

Kelvin.
Morpht

ekendra’s picture

Thanks guys.

This last one worked for me but since I had more suspect chracters ('?', '=') I had to add them. My code ended up looking like:

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

I'm not able to get it to work with any of those functions for Drupal 6. Anyone out there had any success with this in Drupal 6? Just want to replace my # (hash) correctly in php link (keeps coming up with %23 or %2523). Is the url() function better at this than the l() function prehaps? See more details at www.drupal.org/node/255558 .

Thanks,
Andrew G

michaellander’s picture

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

Here is what I used to get passed the has issue. Has anyone found a solution that doesn't involve messing with core? This was my first hack to core, and it kind of stings haha.

calefilm’s picture

sub

codenamerhubarb’s picture

Thanks ekendra, I replaced the old function with your new one in the common.inc file and it solved the problem for me.

-------------------------------
My Drupal site: Download a Book.

dbeall’s picture

I believe pathauto has a punctuation settings page to fix this.
screen shot: http://drupal.org/files/images/pathauto_settings_5x_2x_expanded.png

calefilm’s picture

I've had this problem for some time: http://drupal.org/node/1459158

and the only two pages I've seen %2526 on is common.inc and drupal.js

but i'm no coder and couldn't figure out how to simply cancel out %2526 from showing up. Any suggestions for Drupal 6?

Pathauto will remove the '&' or replace it with a dash, but this is only after saving a Taxonomy Term for instance.. changes PG&E to pg-e. If I set up a panel page argument with a string "Name-dashed", and go to example/pg-e, the url reads example/pg-%2526-e

joshon’s picture

If you are trying to make an anchor tag use fragment:

l("Linked text", "path", $options = array('fragment' => "anchor-name"));

http://api.drupal.org/api/function/l

Heihachi88’s picture

Thanks! That what i needed!

BernardGatt’s picture

If you want to include #tags in path auto, lets say you have ex: /post/_HASH_[node:nid]

just use this snippet below, it will basically look for _HASH_ and replace it with the proper character.

Also works in Drupal 7.

function hook_url_outbound_alter(&$path, &$options, $original_path) {
	if (isset($path))
	{
		$temp_path = drupal_lookup_path('alias',$path);
		$replacement_string = "_HASH_";
		$strip_start = strpos($temp_path, $replacement_string);
		if($strip_start)
		{
			$path = $temp_path;
			$replacement_string_length = strlen($replacement_string);
			$start = $strip_start + $replacement_string_length;
			$length = strlen($path) - $start;
			$after_replacement = substr($path, $start, $length);
			$path = substr($path, 0, $strip_start);
			$options['fragment'] = $after_replacement;
			$options['alias'] = $path;
		}
	}
}

Full blog post here: http://blog.bernardgatt.com/drupal/make-pathauto-generate-links-with-tags/

Jos Ruiz13’s picture

Hi everybody,
I am using the function posted by BernardGatt to replace a string (in this case _HASH_) with a "#" in Pathauto. The function works just i put a string or replacement pattern before _HASH_ ex: /string/_HASH_[node:nid] OR C_HASH_[node:nid]. If I just use _HASH_[node:nid] it wouldnt do the job. I have tried everything in pathauto but I have the feeling I am missing something. Anybody any experience with this ? One more thing is that in the case that the _HASH_ is replaced with the "#" I get the message "Page not found".
Help appreciated.
Thanks

Fabianx’s picture

Here is the most simple solution:

echo l('Title', '#', array('external' => TRUE));

And that works perfectly.

zkent’s picture

We are using D7. Pathauto is installed but not enabled. This solution worked perfectly for us.

tsmulugeta’s picture

What worked for me is adding the following to my template.php file (before the link your trying to use hashtags for or towards the start of the template.php file):

function YOURTHEMENAME_url_outbound_alter(&$path, &$options, $original_path) {
$alias = drupal_get_path_alias($original_path);
$url = parse_url($alias);

if (isset($url['fragment'])){
//set path without the fragment
$path = $url['path'];

//prevent URL from re-aliasing
$options['alias'] = TRUE;

//set fragment
$options['fragment'] = $url['fragment'];
}
}

nwy’s picture

It works best for me but also shows error:

Notice: Undefined index: path

Any solution to fix this?

Ricardo BM’s picture

In drupal 7.23

Hello, I have the same problem, when I try to add a "more..." in a comment into a view the core encode # as %23.

I don't think that it could be a problem to change this, works without encode, but don't works when do that.

The problem is in drupal_http_build_query function

So, in libraries/common.inc replace line 502

- $params[] = $key . '=' . str_replace('%2F','/', rawurlencode($value));

with this one

+ $params[] = $key . '=' . str_replace(array('%2F','%23'),array('/','#'), rawurlencode($value));

green_eye55’s picture

In Drupal 7.52 it is in

includes/common.inc line 502,

but it does not work for me. Is there another solution known?

nwy’s picture

It works if you turn off "Clean URL's"...