简体   繁体   中英

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.

I've tried using html_entity_decode as well as utf8_decode(urldecode())

Current code:

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

Expected return is john@test.com

I suppose your html entity code for character 'n' is wrong. 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 , 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 ENT_COMPAT | ENT_HTML401 (from the PHP docs ) which doesn't convert single quotes. If you need additional flags, you can "add" them using the pipe | symbol like this: ENT_HTML401 | ENT_QUOTES ENT_HTML401 | ENT_QUOTES .

If you're expecting 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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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