steveo225
steveo225

Reputation: 11882

htmlspecialchars except ampersand when starting another entity

I am using htmlspecialchars for printing non-HTML data from an administrative web form. The issue is HTML entities and unicode characters/symbols like ® which prints ® or ★ which prints as ★ due to the ampersand getting converted.

I know I could just replace all & with & to get around this issue, but that isn't appropriate when just printing an ampersand character.

What is a good approach for converting special characters, except ampersands when preceding a symbol?

Upvotes: 2

Views: 3040

Answers (1)

jeroen
jeroen

Reputation: 91734

It sounds like you have a string that already is partially encoded. Encoding it again leads to the problem you describe.

To get around that, you could decode the string first and then encode it again:

$encoded_string = htmlspecialchars(html_entity_decode($partially_encoded_string));

Edit: If all characters need encoding, you can use htmlentities() instead:

$encoded_string = htmlentities(html_entity_decode($partially_encoded_string));

Upvotes: 2

Related Questions