Replace html tags and quotes escapes from text

2,228 views
Skip to first unread message

Eusthace

unread,
Sep 6, 2013, 5:43:40 AM9/6/13
to play-fr...@googlegroups.com
Hi,

I need help again guys, just trying to remove html tags and quotes escapes (quotation marks in html) from a string in my view (scala template).

I'm using:

<p>@str.replaceAll("""<(?!\/?a(?=>|\s.*>))\/?.*?>""", "")</p>

And the problem is that text still includes quotes escapes:

"Tengo un mont&oacute;n de balones de reglamento de un equipo de f&uacute;tbol"

"This sentence is surrounded by &quot; and &quot;, which are a type of quotation marks."

“This sentence is surrounded by &ldquo; and &rdquo;, which are a type of quotation marks.”

‘This sentence is surrounded by &lsquo; and &rsquo;, which are a type of quotation marks.’

«This sentence is surrounded by &laquo; and &raquo;, which are a type of quotation marks.»

‹This sentence is surrounded by &lsaquo; and &rsaquo;, which are a type of quotation marks.›

„This sentence is surrounded by &bdquo; and &ldquo;, which are a type of quotation marks.“

‚This sentence is surrounded by &sbquo; and &lsquo;, which are a type of quotation marks.‘



Any help of how to do it, please?


Johan Andren

unread,
Sep 6, 2013, 9:15:00 AM9/6/13
to play-fr...@googlegroups.com
You can allow raw-html in the string by using the template helper @Html

Don't use it with data that the users can specify though or you are opening up your webapp for injection attacs.

Eusthace

unread,
Sep 6, 2013, 11:41:32 AM9/6/13
to play-fr...@googlegroups.com


It's not what i need, i need to REMOVE html and quotation tags from a text.

Jason Pearson

unread,
Sep 6, 2013, 12:00:23 PM9/6/13
to play-fr...@googlegroups.com
In scala you could write scala.xml.Utility.escape("some text here") and it will escape html entities.  Not sure what the Java method is for that, but try looking at this:




--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Marconi Lanna

unread,
Sep 6, 2013, 1:37:24 PM9/6/13
to play-fr...@googlegroups.com
Don't use a regex, use jsoup: http://jsoup.org/

Eusthace

unread,
Sep 8, 2013, 5:36:39 AM9/8/13
to play-fr...@googlegroups.com


i've tested your solution with no success:

scala.xml.Utility.escape(mymodel.description)

output:

&amp;lt;p&amp;gt;F&amp;amp;uacute;tbol &amp;lt;strong&amp;gt;bal&amp;amp;oacute;n ipsum dolor sit amet&amp;lt;/strong&amp;gt;, consectetur adipiscing elit. Nulla id pharetra elit.

Eusthace

unread,
Sep 9, 2013, 5:07:24 AM9/9/13
to play-fr...@googlegroups.com

Thank you for your help @Marconi, i've finally solve the problem using jsoup.

Solution:

I've added jsoup-1.7.2.jar to the lib folder (you have to create if don't exists) of play project.

Then i've added this line to Build.scala:

,"org.jsoup" % "jsoup" % "1.7.2"

And then i just call parse at my scala template view:

@org.jsoup.Jsoup.parse(mymodel.description).text


Thank you again!

Marconi Lanna

unread,
Sep 9, 2013, 3:47:24 PM9/9/13
to play-fr...@googlegroups.com
I'm glad it worked for you. If you add it to Build.scala, you don't need to put the jar in the lib folder.
Reply all
Reply to author
Forward
0 new messages