
MAGENTO 1.X – FIX CHARSET MESSED UP CHARACTERS
This standalone script will help you to change those weird characters that would appear on descriptions if you made (for example) a database migration.
Let’s create a new script file called <file_name>.php under <MageRoot>/shell/ with the following content.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
require_once(dirname(__FILE__) . '/../app/Mage.php'); require_once 'abstract.php'; class Mage_Shell_FixNonUTFChars extends Mage_Shell_Abstract { private function fixNonUTFChars() { $products = Mage::getResourceModel('catalog/product_collection') ->addAttributeToSelect('sku') ->addAttributeToSelect('description') // The field name that you need to fix. // Uncomment just if you want a particular SKU, or for testing purposes // ->addAttributeToFilter( // 'sku', array('in' => array('sku-1','sku-2','sku-3')) // ) ->load(); foreach ($products as $product) { $description = $product->getData('description'); $changed = false; // Single quotes if (strpos($description, '’') !== false) { $description = str_replace("’", "'", $description); $product->setData('description', $description); $changed = true; } // Double quotes if (strpos($description, 'â€') !== false) { $description = str_replace("â€", "\"", $description); $product->setData('description', $description); $changed = true; } // TradeMark symbol if (strpos($description, 'â„¢') !== false) { $description = str_replace("â„¢", "™", $description); $product->setData('description', $description); $changed = true; } if($changed) { echo $product->getData('sku'). " Changed".PHP_EOL; $product->save(); } } echo "Done"; } public function run() { $this->fixNonUTFChars(); } } Mage::init(); $shell = new Mage_Shell_FixNonUTFChars(); $shell->run(); |
Then, you can run it on your CLI side with:
1 |
php -f shell/<file_name>.php |