Anubhav Anubhav - 5 months ago 15x
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 ==>>

It should be

For reference:


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