在PHP中将UTF-8字符串转换为7位XML

mjs*_*mjs 2 html php xml unicode utf-8

如何将UTF-8字符串(即8位字符串)转换为XML兼容的7位字符串(即带有数字实体的可打印ASCII)?

即一个encode()功能:

encode("“£”") -> "“£”"
Run Code Online (Sandbox Code Playgroud)

decode() 也很有用:

decode("“£”") -> "“£”"
Run Code Online (Sandbox Code Playgroud)

PHP的htmlenties()/ html_entity_decode()对不是正确的事情:

htmlentities(html_entity_decode("“£”")) ->
  "“£”"
Run Code Online (Sandbox Code Playgroud)

费力地指定类型会有所帮助,但仍会返回与XML不兼容的命名实体,而不是数字实体:

htmlentities(html_entity_decode("“£”", ENT_QUOTES, "UTF-8"), ENT_QUOTES, "UTF-8") ->
  "“£”"
Run Code Online (Sandbox Code Playgroud)