How to fully replace all special characters in PHP without leaving any HTML Entity in the result

1.8k views Asked by At

I need help with a PHP replace function I am trying to create.

Basically, I want to FYLLY convert all special characters like á, é, í, ó, ú, ü, ñ, Á, É, Í, Ó, Ú, Ü, Ñ and so on to this: a, e, i, o, u, u, n, A, E, I, O, U, U, N. Below is explained why I say "FULLY convert".

Now I have only managed to do this half way using the below function:

function clean_url($text){
         $text = preg_replace('~&([a-z]{1,10})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($text, ENT_QUOTES, 'UTF-8'));
    return $text;
}

This at first glance gives me the desired result when viewed in MySQL or a browser, so in PHP:

$string = "Ábalos";
echo clean_url($string);

HTML Page source code output: Abalos. This look about right at first glance.

But when I do

$string = "Ábalos";
echo htmlentities(clean_url(($string));

HTML Page source code output: AÂ?balos.

I want to be able to replace with my function also that part Â?. How can this be achieved?

2

There are 2 answers

2
pchmn On BEST ANSWER

I found this function (in this thread : How to remove accents and turn letters into "plain" ASCII characters?) :

function toASCII( $str )
{
    return strtr(utf8_decode($str), 
        utf8_decode(
        'ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ'),
        'SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy');
}

I tested some strings and it works. For example :

function toASCII( $str )
{
    return strtr(utf8_decode($str), 
        utf8_decode(
        'ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ'),
        'SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy');
}

$string = "Ábalos";

echo toASCII($string);

will print Abalos

3
cmorrissey On

you can use iconv to accomplish this.

<?php

    setlocale(LC_ALL, 'en_US.UTF-8');

    $str = "Ábalos";

    echo iconv('UTF-8', 'ASCII//TRANSLIT', $str);

?>