ibaralf
ibaralf

Reputation: 12528

Grails: Replacing symbols with HTML equivalent

I'm reading a CSV file and one of the columns has text that contains symbols that is not recognized. After I read the file, symbols such as ' becomes � . I'm also saving this into a DB.

Obviously when I display this on a webpage, it shows garbage. How can I substitute HTML code (ex. &#180 ;) for this with Grails?

I am reading the CSV using the csv plugin. Code below:

def f = "clientDocs/testfile.csv"
def fReader = new File(f).toCsvMapReader([batchSize:50, charset:'UTF-8'])
fReader.each { batchList ->
batchList.each {
    def description = substituteSymbols(it.Description)


def substituteSymbols(inText) {
    // HOW TO SUBSTITUTE HERE
}

Thanks for any help or suggestions. I've already tried string.replaceAll(regExp).

Upvotes: 1

Views: 2370

Answers (1)

Jan Wikholm
Jan Wikholm

Reputation: 1575

Grails comes with a basic set of encoders/decoders for common tasks.

What you want here is it.Description.encodeAsHTML().

And then if you want the original when displaying in the view, just reverse it with .decodeHTML()

You can read more about these here: http://grails.org/doc/latest/guide/single.html#codecs

(Edited decode method name typo as per the comment)

Upvotes: 4

Related Questions