unicode2iso plugin

Antônio Oliveira edited this page Aug 26, 2016 · 9 revisions
Clone this wiki locally

Category:Contributions::Plugins

The unicode2iso Plugin for CodeIgniter is a simple plugin that allow you to covert an UTF-8 string containing HTML into an ISO-8859-1 string using HTML special chars without stripping the HTML of the original string.

You have to set manually in $retrieve all HTML elements wich you allow to be retrieved.

Plugin home : unicode2iso Plugin

How to use :


<?php
 
$this->load->plugin('unicode2iso');

echo unicode2iso($yourString);
 
?>

Plugin's code :


<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * unicode2iso plugin for CodeIgniter applications
 * @author : Samuel Sanchez, http://www.kromack.com/, April 2009
 * @link : 
 * @license : free
 */


    /**
     * Take an UTF8 string, and return an ISO-8859-1 string with all HTML special chars encoded exept those defined in the $retrieve array.
     *
     * @param string $str
     * @return string
     */
    function unicode2iso($str) {
        
        //Define HTML elements to retrieve after conversion
        $retrieve = array('<br>', '<br />', '<br/>', '<u>',    '</u>', '<i>', '</i>', '<b>', '</b>');
        
        //HTML special chars conversion
        $str = htmlentities($str, ENT_QUOTES, 'UTF-8');
        
        //ISO-8859-1 conversion
        $str = utf8_decode($str);
        
        //Now, we resore our HTML elements
        foreach($retrieve as $row) {
            $str = str_replace(htmlentities($row, ENT_QUOTES), $row, $str);
        }
        
        return $str;        
    }

?&gt;