4

Heres my code:

$string = '&#73&#116';
$string = html_entity_decode($string);
echo $string;

It should be echoing "It", but its just echoing the ASCII codes. Am I using the wrong function? I also tried htmlspecialchars_decode and it changes nothing.

1 Answer 1

9

Those are not valid entities Actually, they are valid in HTML 4 (and I suppose HTML5 too), but in this case the entities need to be semicolon-terminated for PHP to recognize them:

$string = 'It';

htmlspecialchars_decode() only decodes <, >, &, ' and " (and the last two depend on the quotes flag).

Sign up to request clarification or add additional context in comments.

5 Comments

I've tried html_entity_decode($string, ENT_QUOTES, 'ISO-8859-1') and html_entity_decode($string, ENT_QUOTES), neither of them work. The string is on my page using ISO-8859-1. I was using $string = html_entity_decode($string, ENT_QUOTES, 'ISO-8859-1')
@scarhand: Have you tried it with the semicolons? It works for me.
ahhhh! sorry bolt, the semicolons are the problem. i will explode and implode the string with a semicolon. thanks again!
$string = preg_replace('/\&\#([0-9]+)/', '&#$1;', $string); =)
@scarhand: Yeah, I prefer the regex solution.

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.