PHP: Useful Encoding and decoding Functions You Need to Know

List of Encoding and Decoding Functions in PHP
May 18 2022 · 4 min read

As a developer, we are dealing with encoding and decoding very often.

With PHP, like any other language, we can do encoding or decoding on any string, HTML entity, json object, or URL.

PHP provides various built-in functions for that. But sometimes we might not be aware of those functions.

Let’s explore them.


1. HTML

PHP always comes with HTML entities. If there is a requirement to encode/decode HTML entities, here are the solutions.

a. Encode: htmlentities()

This function will convert characters to HTML entities like < , > or .

<?php

$str = "<a href='https://canopas.com'>Let's explore</a>";
echo htmlentities($str);  // in browser
error_log(htmlentities($str)); // in console

 /*
   output
   In browser : <a href='https://canopas.com'>Let's explore</a>
   In console :  &lt;a href=&#039;https://canopas.com&#039;&gt;Let&#039;s explore&lt;/a&gt;
*/

?>

b. Decode: html_entity_decode()

It is a reverse function for htmlentities() . This means it converts HTML entities to characters.

<?php

$str = "&lt;a href=&#039;https://canopas.com&#039;&gt;Let&#039;s explore&lt;/a&gt;";
echo html_entity_decode($str);  // in browser
error_log(html_entity_decode($str)); // in console

/*
   output
   In browser : Let's explore
   In console : <a href='https://canopas.com'>Let's explore</a>
*/

?>

2. UUEncode (Unit-to-Unix-Encoding)

This encoding is a form of binary-to-text conversion in the Unix system. It is used for encoding and decoding files exchanged between users or systems in a network.

PHP also provides a way to convert these types of strings.

a. Encode: convert_uuencode()

This function encodes strings using the uuencode algorithm.

<?php

$str = 'We move fast and break things!!';
error_log(convert_uuencode($str));

# output : ?5V4@;6]V92!F87-T(&%N9"!B<F5A:R!T:&EN9W,A(0``

?>

b. Decode : convert_uudecode()

Again it is the reverse of convert_uuencode() . It decodes the uuencoded string.

<?php

 $str = '?5V4@;6]V92!F87-T(&%N9"!B<F5A:R!T:&EN9W,A(0``';
 error_log(convert_uudecode($str));

# output : We move fast and break things!!

?>

3. URL

PHP provides built-in functions for URL encoding/decoding. Let’s see it.

a. Encode: urlencode()

This function is used to encode the URL and return a string. You can refer to more about it in the PHP official document.

<?php

echo urlencode("https://canopas.com?data=test");
  
# output : https%3A%2F%2Fcanopas.com%3Fdata%3Dtest

?>

b. Decode : urldecode()

This function decodes the string and returns the original URL.

<?php

echo urldecode("https%3A%2F%2Fcanopas.com%3Fdata%3Dtest");
  
# output : https://canopas.com?data=test

?>

4. Base64

Base64 conversion is used for securing data over the network. Here is a nice explanation of what base64 is used for.

PHP has base64_encode and base64_decode functions for that.

a. Encode: base64_encode()

Encodes the given string in Base64 MIME.

<?php

$str = 'We do what matters the most!!';
echo base64_encode($str);

# output : V2UgZG8gd2hhdCBtYXR0ZXJzIHRoZSBtb3N0ISE=

?> 

b. Decode: base64_decode()

Decodes the base64 encoded string.

<?php

$str = 'V2UgZG8gd2hhdCBtYXR0ZXJzIHRoZSBtb3N0ISE=';
echo base64_decode($str);

# output : We do what matters the most!!

?>

5. JSON

If we want to work with array and json in PHP, then it has functions like json_encode and json_decode .Let’s understand their use in PHP.

a. Encode: json_encode()

It encodes a given value in the JSON object.

<?php

# Example 1 (Associative array)

$users = array(array("id"=>"1", "name"=>"John"), array("id"=>"2", "name"=>"Michel"));
echo json_encode($users);

# output: [{"id":"1","name":"John"},{"id":"2","name":"Michel"}]


# Example 2 (Indexed array)

$users = array("John", "Michel");
echo json_encode($users);

# output: ["John","Michel"]

?> 

b. Decode: json_decode()

It is used to decode JSON objects to PHP objects.

<?php

# Example 1 (Associative array)

$users = '[{"id":"1","name":"John"},{"id":"2","name":"Michel"}]';
var_dump(json_decode($users));

/* 
output: array(2) { [0]=> object(stdClass)#1 (2) { ["id"]=> string(1) "1" ["name"]=> string(4) "John" } 
                   [1]=> object(stdClass)#2 (2) { ["id"]=> string(1) "2" ["name"]=> string(6) "Michel" } 
                 }
                 
*/


# Example 2 (Indexed array)

$users = '["John","Michel"]';
var_dump(json_decode($users));

/*
output: array(2) { [0]=> string(4) "John" 
                   [1]=> string(6) "Michel"
                 }
*/

?> 

6. ROT13

ROT13 is a cipher technique that generally replaces a letter with the 13th letter after it in the alphabet. But Numeric and non-alphabetical characters remain untouched.

str_rot13():

If there is a case when we have to encode alphabets only, we can use this method. Because it replaces alphabets only.

<?php

# encode

echo str_rot13("We celebrate differences!!");
# Output 1 : Jr pryroengr qvssreraprf!!


# decode

echo str_rot13("Jr pryroengr qvssreraprf!!");
# Output 2 : We celebrate differences!!

?>
  • Encode: We will pass an original string to str_rot13() and it will output an encoded string.
  • Decode: We will pass the encoded string to str_rot13() and it will output the original string.

7. UTF8

It is the encoding system for Unicode used for computing. It is the standard method for encoding text on the web. HTML has a default charset as UTF-8 .

UTF-8 has been developed to transfer a Unicode character from one computer to another.

a. Encode: utf8_encode()

This function encodes an ISO-8859–1 string to UTF-8. learn more about it on the PHP official document.

<?php

$text = "\xE0";
echo utf8_encode($text);

# output : à

?>

b. Decode: utf8_decode()

This function is used to decode a UTF-8 string to ISO-8859–1.

<?php

$text = "\xE0";
echo utf8_decode($text);

# output : ?
?>

To conclude

That’s it for today, hope you learned something new! The article will be useful in identifying which method you should use in your code for encoding/decoding.

If you know other inbuilt functions for encoding and decoding in PHP, let me know in response section below. I’ll see if I can fit them into the blog.

As always, feedback and suggestions are always welcome.


Related popular articles


sumita-k image
Sumita Kevat
Sumita is an experienced software developer with 5+ years in web development. Proficient in front-end and back-end technologies for creating scalable and efficient web applications. Passionate about staying current with emerging technologies to deliver.


sumita-k image
Sumita Kevat
Sumita is an experienced software developer with 5+ years in web development. Proficient in front-end and back-end technologies for creating scalable and efficient web applications. Passionate about staying current with emerging technologies to deliver.

background-image

Get started today

Let's build the next
big thing!

Let's improve your business's digital strategy and implement robust mobile apps to achieve your business objectives. Schedule Your Free Consultation Now.

Get Free Consultation
footer
Subscribe Here!
Follow us on
2024 Canopas Software LLP. All rights reserved.