HomeCoding & Programming

PHP: Simple function to encode and decode string in php

Like Tweet Pin it Share Share Email

Sometimes you need to store the data in database in encrypted format.

 

No worries, use these below very simple function to encode and decode string in php.
Below are the functions for doing same and is very easy to use, you can customize these as per your requirement to make it very strong encoded.

 

PHP Function for encrypt a string

<?php
    /**
    * Function to encrypt the string
    * will provide you a very strong encoded string
    */

    function encode5t($str)
    {
		for($i=0; $i<5;$i++){
			//apply base64 first and then reverse the string
			$str=strrev(base64_encode($str));
		}
			return $str;
    }
?>

Function to decrypt the encrypted string

<?php
    /**
    * Function to decrypt the string (that was previously encoded)
    */

    function decode5t($str)
    {
		for($i=0; $i<5;$i++){
			//apply reverse the string first and then base64
			$str=base64_decode(strrev($str));
		}
			return $str;
    }
?>

Comments (1)

Comments are closed.