Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace characters with HTML entities in Java

I want to replace certain characters with their respective HTML entities in an HTML response inside a filter. Characters include <, >, &. I can't use replaceAll() as it will replace all characters, even those that are part of HTML tags.

What is the best approach for doing so?

like image 216
user1448652 Avatar asked Dec 12 '22 01:12

user1448652


1 Answers

From Java you may try Apache Commons Lang (legacy v2) StringEscapeUtils.escapeHtml(). Or with commons-lang3: StringEscapeUtils.escapeHtml4().

Please note this also converts à to &agrave; & such.

like image 88
sangupta Avatar answered Jan 03 '23 21:01

sangupta