php 如何在php中将字符串转换为utf-8代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13342204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:13:02  来源:igfitidea点击:

How to convert a string to utf-8 code in php

php

提问by Moustapha Rifai

i need a function or a simple algo to help me convert a normal string to utf-8 code

我需要一个函数或一个简单的算法来帮助我将普通字符串转换为 utf-8 代码

ex: string:

例如:字符串:

hello
????

UTF-8 CODE:

UTF-8 代码:

68 65 6C 6C 6F 0A 0639 0631 0628 064A 0A

回答by Omar Freewan

use the utf8_encode ( $data )

使用 utf8_encode ( $data )

see its full documentation http://php.net/manual/en/function.utf8-encode.php

查看其完整文档http://php.net/manual/en/function.utf8-encode.php

回答by Adam

Use iconv, as shown:

使用iconv,如图:

$text = iconv(mb_detect_encoding($text, mb_detect_order(), true), "UTF-8", $text);

This is also detects the strings current character set.

这也检测字符串当前字符集。

回答by Anton Sementsov

Try to use iconvfuncion here is description

尝试使用iconvfuncion这里描述

回答by Mohammad Ahmad

if you want to use it with arabic

如果你想和阿拉伯语一起使用

you can use arabic php library at : http://ar-php.org/

您可以在以下位置使用阿拉伯语 php 库:http: //ar-php.org/

回答by SajjadHashmi

Try This:

尝试这个:

...
$somStr = 'Hello';
utf8_encode_deep($somStr); // Converting string to utf8
print_r($somStr);
...

For more details regarding utf8_encode_deep()see this

有关更多详细信息,utf8_encode_deep()请参阅

Hope it solves this problem.

希望它能解决这个问题。