user3132858 user3132858 - 3 months ago 13
PHP Question

How to FULLY replace all special characters in PHP wihtout leaving any HTML Entity in the result

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?

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