Anubhav Anubhav - 6 months ago 27
PHP Question

Not getting desired output from iconv

function getCleanUrl($str, $replace=array(), $delimiter='-'){
setlocale(LC_COLLATE, 'en_GB');
if(!empty($replace)) {
$str = str_replace((array)$replace, ' ', $str);
}

$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
$clean = strtolower(trim($clean));
$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);

return $clean;

}


When I make call to the above function:

$str = "cTänk efter nu – förr'n vi föser dig bort";
$replace = array("'","!",".");
$cleanString = getCleanUrl($str,$replace);


OUTPUT: $cleanString ==>>
ctnk-efter-nu-frr-n-vi-fser-dig-bort


It should be
ctank-efter-nu-forr-n-vi-foser-dig-bort


For reference: http://cubiq.org/the-perfect-php-clean-url-generator

Answer

The problem is in your setlocale() line. The locale specified probably doesn't exist. Try this:

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

If not, try:

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

To list locales installed on your system, execute the following by the command line:

$ locale -a
Comments