Hash/unhash HTML

Here’s a little class I wrote that lets you hash HTML and other things so that you can do some processing on just the text, and then unhash the HTML again.

class ht
{
        static $hashes = array();

        # hashes everything that matches $pattern and saves matches for later unhashing
       function hash($text, $pattern) {
                return preg_replace_callback($pattern, array(self,'push'), $text);
        }

        # hashes all html tags and saves them
       function hash_html($html) {
                return self::hash($html, '`<[^>]+>`');
        }

        # hashes and saves $value, returns key
       function push($value) {
                if(is_array($value)) $value = $value[0];
                static $i = 0;
                $key = "\x05".++$i."\x06";
                self::$hashes[$key] = $value;
                return $key;
        }

        # unhashes all saved values found in $text
       function unhash($text) {
                return str_replace(array_keys(self::$hashes), self::$hashes, $text);
        }

        function get($key) {
                return self::$hashes[$key];
        }

        function clear() {
                self::$hashes = array();
        }
}
Posted in

Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *