php utf8转unicode的方法:首先创建一个PHP示例文件;然后通过“iconv($encoding, 'UCS-2BE', $str);”方法实现转换即可。
推荐:《PHP视频教程》
linux下php中文UTF-8转换Unicode方法和注意事项
先说下遇到问题:1.php没有内置unicode_ecode函数可以直接使用
2.网上很多资料都是用$str
= iconv(
$encoding
,
'UCS-2'
,
$str
);
window下转换出来的是正常的,但在Linux下转换出来的两个字符是相反的,用在线unicode转换工具出来的结果是乱码。
UCS-2的编码规则:
windows下默认是UCS-2LE。
linux下默认是UCS-2BE。用iconv(指定UCS-2)来转换生成的是UCS-2BE的unicode,但可能php环境配置会导致不是UCS-2BE。
windows和linux等多个平台对 UCS-2 的理解不同(UCS-2LE,UCS-2BE),所以为了统一需要直接指定为UCS-2BE。
即把:$str
= iconv(
$encoding
,
'UCS-2'
,
$str
); 改为<code class="php variable">$str
= iconv(
$encoding
,
'UCS-2BE'
,
$str
);
亲测转换出来的unicode可以正常转换的
下面是两个本人亲测可以使用的函数(为了避免以后跟系统新的内置函数同名在前面加了个my前缀):
/** * utf-8 转unicode * @param string $name * @return string */ function myutf8_unicode($name){ $name = iconv('UTF-8', 'UCS-2BE', $name); $len = strlen($name); $str = ''; for ($i = 0; $i < $len - 1; $i = $i + 2){ $c = $name[$i]; $c2 = $name[$i + 1]; if (ord($c) > 0){ $str .= '\u'.base_convert(ord($c), 10, 16).str_pad(base_convert(ord($c2), 10, 16), 2, 0, STR_PAD_LEFT); } else { $str .= '\u'.str_pad(base_convert(ord($c2), 10, 16), 4, 0, STR_PAD_LEFT); } } return $str; } /** * unicode 转 utf-8 * * @param string $name * @return string */ function myunicode_decode($name) { $name = strtolower($name); // 转换编码,将Unicode编码转换成可以浏览的utf-8编码 $pattern = '/([\w]+)|(\\\u([\w]{4}))/i'; preg_match_all($pattern, $name, $matches); if (! empty($matches)) { $name = ''; for ($j = 0; $j < count($matches[0]); $j ++) { $str = $matches[0][$j]; if (strpos($str, '\\u') === 0) { $code = base_convert(substr($str, 2, 2), 16, 10); $code2 = base_convert(substr($str, 4), 16, 10); $c = chr($code) . chr($code2); $c = iconv('UCS-2BE', 'UTF-8', $c); $name .= $c; } else { $name .= $str; } } } return $name; }
测试代码:
$ustr = myutf8_unicode('我的新衣'); echo '我的新衣:'.$ustr.'<br>'; $str = myunicode_decode($ustr); echo $str.'<br>';
输出结果:
在站长工具里可以正常转换,说明没有问题。
The above is the detailed content of How to convert php utf8 to unicode. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version
SublimeText3 Linux latest version