user2635901
user2635901

Reputation: 223

Converting to UTF8 in Laravel project not working

I am running this line of code.

$string = convert_case('MONTAÑA 221');

public function convert_case($string)
{
    $string = mb_convert_case($string, MB_CASE_TITLE, "UTF-8");

    return $string;
}

When I run it in laravel I get empty string but when I run it in the browser using just a simple test.php file over wamp, I get the correct string.

Upvotes: 0

Views: 1973

Answers (2)

Andre F.
Andre F.

Reputation: 1163

To address your question based on the code you've posted:

                             $string = 'MONTAÑA 221';
                                |
                                v
public function convert_case($string)
{   
    $string = html_entity_decode($input,ENT_COMPAT,"UTF-8");

    $oldLocale = setlocale(LC_CTYPE, '0');  

    setlocale(LC_CTYPE, 'en_US.UTF-8');
    $string = iconv("UTF-8","ASCII//TRANSLIT",$string);

    setlocale(LC_CTYPE, $oldLocale);

    return strtolower(preg_replace('/[^a-zA-Z0-9]+/','',$string));
}

Output:

montana221

Code in the space where ever you feel they're needed. This code is specific to running on the backend. Using HTML's:

<meta charset="utf-8">

Could be the reason why it comes out correctly on the front-end.

Upvotes: 1

Matt
Matt

Reputation: 1186

I have seen issues like this before, and maybe yours is different, but setting the encoding of the HTML page may be useful to you.

In the header:

<meta charset="utf-8" /> 

Upvotes: 1

Related Questions