Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2011-01-23 17:03:03

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,483
Bitbucket GitHub

Sanest method for encoding a URL

Hi there. A quick and easy question.

I’m using <txp:permlink /> to output a text string like this:

http://example.com/section/article

What I’d like to do is take that <txp:permlink />’d URL and encode/flatten it into this format:

http%3A%2F%2Fexample.com%2Fsection%2Farticle

I was thinking I could use rah_replace to switch the characters:

<txp:rah_replace from=":,/" to="%3A,%2F"><txp:permlink /></txp:rah_replace>

…but is there a recommended saner/cleaner option to percent encode, plugin or otherwise?

Thanks in advance.
-Pete

Last edited by gaekwad (2011-01-23 17:14:03)

Offline

#2 2011-01-23 17:52:16

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 5,001
Website GitHub

Re: Sanest method for encoding a URL

Could you not use txp:php and a php snippet passing your permlink through php’s urlencode ?

Something like this (untested):

<txp:php>
   global $thisarticle;
   echo urlencode( permlinkurl_id($thisarticle['thisid']) );
</txp:php>

or use echo urlencode($thisarticle['url-title']); if you just need the url_title encoded.

Alternatively, if you don’t mind using a plugin, use Jukka’s rah_function plugin to call the php command (again untested):

<txp:rah_function call="urlencode"><txp:permlink /></txp:rah_function>

BTW: Check the notes on the php docs page linked above if you have the ‘&’-sign in your urls.

Last edited by jakob (2011-01-23 18:05:20)


TXP Builders – finely-crafted code, design and txp

Offline

#3 2011-01-24 09:01:59

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,483
Bitbucket GitHub

Re: Sanest method for encoding a URL

Thanks, jakob – very useful information :)

Offline

Board footer

Powered by FluxBB