如何防止替换字符变成PHP中的html实体? [英] How can I keep the replacement character from turning into an html entity in PHP?

查看:68
本文介绍了如何防止替换字符变成PHP中的html实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理替换字符 MySQL数据库...,如果它在那里就可以了,但是我正在尝试对其进行编辑。我的表单将字符显示为带有问号的菱形(�)。因此,我提交了表单,将表单上的数据与数据中的数据进行了比较,以查看其是否已更改。这里的问题是,当我提交表单时,它将替换字符转换为& #65533; ,这是html实体的等价物,因此在发生这种情况时,它无法进行比较,并且代码认为字符串已更改-它已经更改了,但实际上并没有。我尝试采用不同的方法,要么在比较时将替换字符从数据库中转换为html实体等价物-它将开始将另一个看似正常的字符转换为另一个html实体等效物,然后将html实体替换字符-这根本不起作用-但它们都失败了。是的,我尝试了html_entity_decode()和htmlspecialchars_decode()

I'm dealing with a replacement character inside a MySQL database... and it's fine if it stays there but I'm trying to edit it. My form displays the character as a diamond shape with a question mark in it (�). So I submit the form, I compare the data between the one on the form to the one in the data to see if it has changed. The problem here is that when I submit the form it turns the replacement character into � which is the html entity equivalent so when this happens it fails the comparison and the code thinks the string has changed-- which it has, but not really. I've tried to employ different methods of either turning the replacement character into the html entity equivalent from the database when it's being compared --it starts to turn another seemingly normal characters into another replacement character html entity equivalent-- and turning the html entities into the replacement character --which simply does not work for this-- but they both fail. And yes, I have tried html_entity_decode() and htmlspecialchars_decode()

我的问题是:如何防止替换字符变成html实体?

My questions is: How can I keep the replacement character from turning into an html entity?

推荐答案

由于某种原因,网络浏览器提交的是REPLACEMENT CHARACTER(U + FFFD),为十进制数字HTML实体:�

For some reason, the webbrowser is submitting the � REPLACEMENT CHARACTER (U+FFFD) as it's decimal, numeric HTML Entitiy: �. Probably you're already outputting it that way to the browser?

但是,如果您希望输入包含HTML实体,那么您需要对其进行解码,如果不包含HTML实体,则需要对其进行解码想要将它们作为HTML存储到数据库中。解码传入的UTF-8编码字符串 $ str 中的数字实体:

However, if you expect the input to contain HTML entities, you need to decode them if you don't want to store them into your database as HTML. To decode numeric entities within an incomming UTF-8 encoded string $str:

$convmap = array (0, 0x10FFFF, 0, 0xFFFFFF);
$output = mb_decode_numericentity($str, $convmap, 'UTF-8');

此代码实际上可以完成您要查找的转换(演示),但是您应该首先说明为什么要提交数字HTML实体。

This code does actually do the conversion you're looking for (Demo), however you should clarify first why a numeric HTML entity is submitted.

您更喜欢unicode,建议您在网页上使用UTF-8:

As you prefer unicode, I suggest you make use of UTF-8 for the webpage:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

,格式为:

<form action="" method="post" accept-charset="utf-8">

祝你好运。

这篇关于如何防止替换字符变成PHP中的html实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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