HTML Encrypt Class with Php

Hello, in today's article I will talk about the class of encryption of HTML codes.

<?php

$html = '<!doctype html>
<html>
<head>
  <meta charset="utf8">
  <title>Example</title>
</head>
<body>

  <h1>Welcome Test Page</h1>

</body>
</html>';

echo HTML_Encrypt::encrypt($html);

/**
  output:
  
  <script type="text/javascript">document.write(unescape("%3c%21%64%6f%63%74%79%70%65%20%68%74%6d%6c%3e%20%20%3c%68%74%6d%6c%3e%20%20%3c%68%65%61%64%3e%20%20%20%20%3c%6d%65%74%61%20%63%68%61%72%73%65%74%3d%22%75%74%66%38%22%3e%20%20%20%20%3c%74%69%74%6c%65%3e%45%78%61%6d%70%6c%65%3c%2f%74%69%74%6c%65%3e%20%20%3c%2f%68%65%61%64%3e%20%20%3c%62%6f%64%79%3e%20%20%20%20%20%20%3c%68%31%3e%57%65%6c%63%6f%6d%65%20%54%65%73%74%20%50%61%67%65%3c%2f%68%31%3e%20%20%20%20%3c%2f%62%6f%64%79%3e%20%20%3c%2f%68%74%6d%6c%3e"));</script>

**/

?>

for class;

class HTML_Encrypt
{

    /**
     * @param $str
     * @param int $len
     * @return array
     *
     * Splits the word according to utf8 characters.
     */
    public static function utf8_split($str, $len = 1)
    {
        $arr = array();
        $strLen = mb_strlen($str, 'UTF-8');
        for ($i = 0; $i < $strLen; $i++) {
            $arr[] = mb_substr($str, $i, $len, 'UTF-8');
        }
        return $arr;
    }

    /**
     * @param $character
     * @return mixed
     *
     * Returns the ASCII value of each character in utf8.
     */
    public static function utf8_ord( $character )
    {
        list(, $ord) = unpack('N', mb_convert_encoding($character, 'UCS-4BE', 'UTF-8'));
        return $ord;
    }


    /**
     * @param $html
     * @return string
     *
     * Conversion of the decimal number system to the hexadecimal number system.
     */
    public static function encrypt( $html )
    {
        $html = str_replace(array("\n","\r","\t"), ' ', $html);
        $hex = '<script type="text/javascript">document.write(unescape("';
        $characters = self::utf8_split($html);
        foreach ( $characters as $character ){
            $hex .= '%' . dechex(self::utf8_ord($character));
        }
        $hex .= '"));</script>';
        return $hex;
    }

}

Although we defend open source code writing, sometimes it is necessary to hide it :)

Comments

There are no comments, make the firs comment