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.
We are what we repeatedly do. Excellence, then, is not an act, but a habit. Try out Justly and start building your habits today!
PHP always comes with HTML entities. If there is a requirement to encode/decode HTML entities, here are the solutions.
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 : <a href='https://canopas.com'>Let's explore</a>
*/
?>
It is a reverse function for htmlentities()
. This means it converts HTML entities to characters.
<?php
$str = "<a href='https://canopas.com'>Let's explore</a>";
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>
*/
?>
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.
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``
?>
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!!
?>
PHP provides built-in functions for URL encoding/decoding. Let’s see it.
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
?>
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
?>
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.
Encodes the given string in Base64 MIME.
<?php
$str = 'We do what matters the most!!';
echo base64_encode($str);
# output : V2UgZG8gd2hhdCBtYXR0ZXJzIHRoZSBtb3N0ISE=
?>
Decodes the base64 encoded string.
<?php
$str = 'V2UgZG8gd2hhdCBtYXR0ZXJzIHRoZSBtb3N0ISE=';
echo base64_decode($str);
# output : We do what matters the most!!
?>
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.
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"]
?>
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"
}
*/
?>
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.
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!!
?>
str_rot13()
and it will output an encoded string.str_rot13()
and it will output the original string.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.
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 : à
?>
This function is used to decode a UTF-8 string to ISO-8859–1.
<?php
$text = "\xE0";
echo utf8_decode($text);
# output : ?
?>
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.
We’re Grateful to have you with us on this journey!
Suggestions and feedback are more than welcome!
Please reach us at Canopas Twitter handle @canopas_eng with your content or feedback. Your input enriches our content and fuels our motivation to create more valuable and informative articles for you.
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 ConsultationGet 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