Magento 1 Php7 Core Bug and Fix

We recently came across an issue with a client that had previously upgraded to PHP7 from PHP5.x for their Magento 1 CE store. The issue came to light when the client tried to create a new store in the admin and found that their store name was left blank when they hit the save button.

No errors were displayed as the site was in production mode and the logs did not give any useful information. After looking in to this locally with developer mode I found that a core Magento class was still using deprecated code even after using a PHP7 compatibility module.

Unfortunately there are only two ways of fixing the issue discovered, the first and correct option is to update Magento as the bug is present in all versions of Magento prior to Magento 1.9.1.0, there does not seem to be any patches that resolve the issue officially.

The second faster (and possibly safer depending on how big your site is) is to directly edit the core file (!!).

As all developers know, editing the core is taboo but in this case we are looking at bringing code from a later version of Magento that has fixed the issue.

The issue boils down to the use of the preg_replace function with the e modifier, the code is used across the entire installation when calling the removeTags method on any helper that extends from Mage_Core_Helper_Abstract.

To fix the issue without doing a Magento upgrade, replace the removeTags() method with the updated version

    public function removeTags($html)
    {
        $html = preg_replace_callback(
            "# <(?![/a-z]) | (?<=\s)>(?![a-z]) #xi",
            function ($matches) {
                return htmlentities($matches[0]);
            },
            $html
        );
        $html =  strip_tags($html);
        return htmlspecialchars_decode($html);
    }

Always be sure to test your changes before deploying to production, it is also recommended you comment your edit loudly so the change is not overlooked. E.g.

############### Edit Made #################

Tags: magentomagento1bug fixcorehelperremoveTagsphp7php