URL Functions

A few functions that work with URL strings to encode, decode, parse and generally make sure they are in the correct format for transmission over the internet.

 

urlencode()

Since URLs can only be transmitted over the internet using ASCII characters, it is often necessary to convert non-alphanumeric characters to their ASCII coded equivalent

  • Encodes a string to be used in query part of URL
  • Convenient way to pass variables to other pages
  • Returns string in which all non-alphanumeric characters except -_. have been replaced by the percent sign followed by two hex digits and spaces encoded as +

Save & refresh browser:

What's this doing here?
What%27s+this+doing+here%3F
test

 

Mousing over the test url will show the non-alphanumeric characters in the link have been converted to their ASCII equivalent codes:

https://tech-academy.co.uk/someService?myVar=What%27s+this+doing+here%3F

 

 

urldecode()

  • Decodes any ASCII two digit hex %## encoding in the given string
  • Plus symbols ('+') are decoded to a space character

Save & refresh browser:

https://tech-academy.co.uk/someService?myVar=What%27s+this+doing+here%3F
https://tech-academy.co.uk/someService?myVar=What's this doing here?

 

 

rawurlencode()

  • Similar to above but converts spaces to %20

rawurldecode()

  •  Decodes above rawurlencoded string

Save & refresh browser:

Isn't this a string with % non-alphanumeric $ * £ characters?
Isn%27t%20this%20a%20string%20with%20%25%20non-alphanumeric%20%24%20%2A%20%C2%A3%20characters%3F
test
Isn't this a string with % non-alphanumeric $ * £ characters?

 

Mousing over the test url will show the non-alphanumeric characters in the link have been converted to their ASCII equivalent codes:

https://derrickrobinson.com/php/someService?myVar=Isn%27t%20this%20a%20string%20with%20%25%20non-alphanumeric%20%24%20%2A%20%C2%A3%20characters%3F

 

base64_encode() / base64_decode()

  • Encodes the given data with base64
  • This encoding is designed to make binary data survive transport through transport layers that are not 8-bit clean, such as mail bodies
  • Base64-encoded data takes about 33% more space than the original data

Save & refresh browser:

This string is being base64 encoded / decoded
VGhpcyBzdHJpbmcgaXMgYmVpbmcgYmFzZTY0IGVuY29kZWQgLyBkZWNvZGVkOiA8YnI+

 

Now decoding:
This string is being base64 encoded / decoded

 

More URL functions here.

Leave a Reply