[英]Escape html anchor tags with [href]
How to escape each html dynamic anchor tag if URLs are different? 如果网址不同,如何转义每个html动态锚标记?
<a href="*_url">*_url</a>
I need the above code to be saved in the database as 我需要上面的代码保存为数据库
<a href="*_url">*_url</a>
I've escaped other tags like <code>
, <html>
, <strong>
... using 我使用了
<code>
, <html>
, <strong>
等其他标签,
`StringUtils.replaceEach(post, search, replacement);`
Because they are static tags. 因为它们是静态标签。 But the anchor tag holds
"href"
as dynamic content. 但是锚标记将
"href"
保留为动态内容。
How can I get this done by a Java
method at once. 如何立即通过
Java
方法完成此操作。
Thanks! 谢谢!
使用Apache Commons Lang中的 StringEscapeUtils.escapeHtml()
。
You can use java.net.URLEncoder.encode on conjunction with your StringUtils.replaceEach . 您可以将java.net.URLEncoder.encode与StringUtils.replaceEach结合使用。 You may want to parse the anchor tag and encode the same using URLEncoder .
您可能需要解析锚标记并使用URLEncoder对其进行编码。
David Grant's solution seems correct to me. David Grant的解决方案对我来说似乎是正确的。 In the meantime
escapeHtml
has gotten obsolete. 同时,
escapeHtml
已过时。 You can choose between escapeHtml3
and escapeHtml4
. 您可以在
escapeHtml3
和escapeHtml4
之间进行选择。
1) Add to Your Gradle Dependency List: 1)添加到您的Gradle依赖列表:
compile 'org.apache.commons:commons-lang3:3.3.2'
For Maven it is: 对于Maven,它是:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
2) 2)
import org.apache.commons.lang3.StringEscapeUtils;
3) Then invoke... 3)然后调用...
StringEscapeUtils.escapeHtml4(...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.