Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert variable (text/string) to utf8mb4 in php

Hi I'm looking for a encode function for utf8mb4,

$var = = "نور";

echo utf8mb4_encode($string);

output = نور // its $var output in UTFMB4

The output should be "نور" this, its a conversion of $var in utfmb4

like image 408
Mukhyyar Avatar asked Oct 22 '17 12:10

Mukhyyar


People also ask

How to set php to UTF-8?

The first thing you need to do is to modify your php. ini file to use UTF-8 as the default character set: default_charset = "utf-8"; (Note: You can subsequently use phpinfo() to verify that this has been set properly.)

What is UTF-8 php?

Definition and Usage. The utf8_encode() function encodes an ISO-8859-1 string to UTF-8. Unicode is a universal standard, and has been developed to describe all possible characters of all languages plus a lot of symbols with one unique number for each character/symbol.

What does mb_ convert_ encoding do?

Converts string from from_encoding , or the current internal encoding, to to_encoding . If string is an array, all its string values will be converted recursively.

How do I change utf8mb4 to UTF-8?

To solve the problem open the exported SQL file, search and replace the utf8mb4 with utf8 , after that search and replace the utf8mb4_unicode_520_ci with utf8_general_ci . Save the file and import it into your database. After that, change the wp-config. php charset option to utf8 , and the magic starts.


2 Answers

mb_convert_encoding is the answer. another option is the iconv function - but this is assuming $var is not already in utf8 - you must first find out what characterset $var is encoded in. and if your variable is indeed hardcoded into the PHP script itself, then either:

it's already utf-8 encoded

OR

your php script starts with

<?php
declare(encoding='ISO-8859-1');

(just replace ISO-8859-1 with whatever the actual encoding is)

OR

its a bug in your source code. (because PHP source code is UTF-8 encoded by default, unless otherwise specified with the encoding declaration)

assuming ISO-8859-1, $result = mb_convert_encoding($string, 'UTF-8', 'ISO-8859-1'); / $result=iconv('ISO-8859-1','UTF-8',$string);

(PS, utf8mb4 is NOT a character encoding, utf8mb4 is just MySQL's nickname for utf8. what MySQL calls utf8 is actually a 3-byte subset of the real utf8. and what MySQL calls utf8mb4 is the real utf8. its just some MySQL brain-damage. and unfortunately, MariaDB inherited this brain-damage from MySQL when it was forked.)

like image 148
hanshenrik Avatar answered Oct 23 '22 23:10

hanshenrik


The return value of the mb_convert_encoding function could give you the desired result.

$string = "نور";
$result = mb_convert_encoding($string, 'UTF-8', 'Windows-1252');
//$result = mb_convert_encoding($string, 'UTF-8', 'Windows-1254');
echo $result;
like image 5
odan Avatar answered Oct 24 '22 00:10

odan