简体   繁体   English

使用PHP解码字符串

[英]Using PHP to decode a string

I'm trying to decode a string using PHP but it doesn't seem to be returning the correct result. 我正在尝试使用PHP解码字符串,但它似乎未返回正确的结果。

I've tried using html_entity_decode as well as utf8_decode(urldecode()) 我试过使用html_entity_decode以及utf8_decode(urldecode())

Current code: 当前代码:

$str = "joh'@test.com";
$decodeStr = html_entity_decode($str, ENT_COMPAT, "UTF-8");

Expected return is john@test.com 预期收益为john@test.com

I suppose your html entity code for character 'n' is wrong. 我想您的字符“ n”的html实体代码是错误的。 Working example: 工作示例:

$str = "john@test.com";
echo $decodeStr = html_entity_decode($str, ENT_COMPAT, "UTF-8");

Shouldn't the entity for @ be @ @的实体不应该@ instead of ' 而不是' which is for an apostrophe? 哪个是撇号?

The HTML entity code for n is n n的HTML实体代码为n , whereas the entity code in your string is for a single apostrophe ' . ,而您字符串中的实体代码仅用于单个“撇号' If you wanted to convert single quotes, the ENT_QUOTES flag must be used when calling html_entity_decode() , as the default is ENT_COMPAT | ENT_HTML401 如果要转换单引号,则在调用html_entity_decode()时必须使用ENT_QUOTES标志,因为默认值为ENT_COMPAT | ENT_HTML401 ENT_COMPAT | ENT_HTML401 (from the PHP docs ) which doesn't convert single quotes. ENT_COMPAT | ENT_HTML401 (来自PHP docs )不会转换单引号。 If you need additional flags, you can "add" them using the pipe | 如果需要其他标志,可以使用管道“添加”它们| symbol like this: ENT_HTML401 | ENT_QUOTES 像这样的符号: ENT_HTML401 | ENT_QUOTES ENT_HTML401 | ENT_QUOTES . ENT_HTML401 | ENT_QUOTES

If you're expecting john@test.com : 如果您期望john@test.com

$str = "john@test.com";
$decodeStr = html_entity_decode($str, ENT_COMPAT, "UTF-8");
echo $decodeStr;  // john@test.com

Or if you're expecting joh'@test.com : 或者,如果您期望joh'@test.com

$str = "joh'@test.com";
$decodeStr = html_entity_decode($str, ENT_QUOTES, "UTF-8");
echo $decodeStr; // joh'@test.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM