1

I'm getting some data from the database and it has HTML Encoded chars ( ). What options are there for removing these?

I don't want these rendered at all...I want them stripped from the data.

At the moment I'm not worried about the HTML tags...just the encoded characters.

EDIT: If it's relevant these chars are causing some errors in JSON validation.

3
  • Can you prevent them from going in, in the first place? Commented Jan 1, 2013 at 22:09
  • How are you outputting the JSON? It seems to me like a JSON library should be able to easily handle the encoding/decoding of HTML encoded characters. Commented Jan 1, 2013 at 22:11
  • @ErikPhilips not really they are needed in certain places, but in a datatable.net table they are causing issues. Commented Jan 1, 2013 at 22:26

2 Answers 2

1

If you want to get rid of them, obtain a list of such characters or a RegExp matching them all (something like &[a-z]+;) and do a search-and replace.

However, if you only want them gone due to errors in JSON validation, you should correctly generated/encode your JSON to avoid the errors. (However, I don't really understand how they can cause invalid JSON.)

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

2 Comments

Whenever it's getting encoded/used by datatables.net it's causing the validation issue (I believe). Parse error on line 8: ...ado", ""BeginAllYourAdventur ----------------------^ Expecting '}', ':', ',', ']'
Somehow, a quote character " got in there, ending the string and messing up the JSON. & and ; have no special meaning in JSON, " does. I'd suggest you create a minimal example demonstrating the full issue (including the generated JSON), as I believe it does not have much to do with HTML entities in general.
0

Simply trimming by regexp should not be an option here. For example &nbsp; can be coded as &#160; as well, but &\#[0-9]+; regex would lead to data loss, since almost every char can become encoded like that at some point (ex.:<p>&#72;&#69;&#76;&#76;&#79;</p>).

Comments

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.