将html实体转换为UTF-8,但保留现有的UTF-8 [英] Convert html entities to UTF-8, but keep existing UTF-8

查看:74
本文介绍了将html实体转换为UTF-8,但保留现有的UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将html实体转换为UTF-8,但是 mb_convert_encoding 会销毁已经由UTF-8编码的字符.正确的方法是什么?

I want to convert html entities to UTF-8, but mb_convert_encoding destroys already UTF-8 encoded characters. Whats the correct way?

$text = "äöü ä ö ü ß";
var_dump(mb_convert_encoding($text, 'UTF-8', 'HTML-ENTITIES'));
// string(24) "äöü ä ö ü ß"

推荐答案

mb_convert_encoding()不是您要实现的正确功能:您应该真正使用 html_entity_decode()代替,因为它只会转换实际的html实体改为UTF-8,并且不会影响字符串中现有的UTF-8字符.

mb_convert_encoding() isn't the correct function for what you're trying to achieve: you should really be using html_entity_decode() instead, because it will only convert the actual html entities to UTF-8, and won't affect the existing UTF-8 characters in the string.

$text = "äöü ä ö ü ß";
var_dump(html_entity_decode($text, ENT_COMPAT | ENT_HTML401, 'UTF-8'));

给出

string(18) "äöü ä ö ü ß"

演示

这篇关于将html实体转换为UTF-8,但保留现有的UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆