Ahmed iqbal
Ahmed iqbal

Reputation: 597

In Codeigniter how to decode UTF-8 hex string into proper English

I have search feature with codeigniter framework, today i've noticed some peoples are passing character strings like Á, ú, ç, é

When input passed by codeigniter core input, it converts into %C3%81, %C3%BA, %C3%A7, %C3%A9 , i used following codeigniter helper.

$this->load->helper('text');

convert_accented_characters('è'); // output: e

it convert è into e, but how i can convert UTF-8 - HEX '%C3%A9' or 'c3a9' into proper English character like A, u, c, e

Upvotes: 0

Views: 1346

Answers (2)

iLaYa  ツ
iLaYa ツ

Reputation: 4017

If your PHP version is greater than or equal to 5.4, you can try Transliterator class.

$transliterator = Transliterator::createFromRules(':: NFD; :: [:Nonspacing Mark:] Remove; :: NFC;', Transliterator::FORWARD);
    $test = ['Á', 'ú', 'ç', 'é','Áúçé'];
    foreach($test as $e) {
        $normalized = $transliterator->transliterate($e);
        echo $e. ' --> '.$normalized."\r\n";
    }

Output:

Á --> A ú --> u ç --> c é --> e Áúçé --> Auce 

*By default it will be disabled. It should be enabled in php.ini file to use this feature.

Update:

Seems like your data is URL encoded. So you have to use urlencode function

urldecode("%C3%81, %C3%BA, %C3%A7, %C3%A9"); // Á, ú, ç, é

Then you can use CI feature to convert è into e, already you are using it

convert_accented_characters('è'); // output: e

Upvotes: 2

Ben Fried
Ben Fried

Reputation: 2204

You can use PHP's built-in function for UTF-8 decoding:

utf8_decode(string);

Upvotes: 0

Related Questions