0

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 [email protected]

3
  • 2
    how did you encode it ? It seems your original string was joh'@test.com. Commented Feb 7, 2019 at 5:50
  • It was encoded by the cms that I'm using - im not sure of the specifics of the encoding. Commented Feb 7, 2019 at 6:03
  • without knowing the encoding rule, how can you decode it ? Commented Feb 7, 2019 at 6:11

3 Answers 3

3

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");
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply - no it's correct that it should be an apostrophe.
If "it's correct that it should be an apostrophe", how do we interpret your "Expected return is [email protected]"?
0

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 (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.

If you're expecting [email protected]:

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

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

Comments

0

Shouldn't the entity for @ be @ instead of ' which is for an apostrophe?

1 Comment

Thanks for the reply - no it's correct that it should be an apostrophe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.