简体   繁体   English

使用[href]转义html锚标记

[英]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 我需要上面的代码保存为数据库

&lt;a href="*_url"&gt;*_url&lt;/a&gt;

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.encodeStringUtils.replaceEach结合使用。 You may want to parse the anchor tag and encode the same using URLEncoder . 您可能需要解析锚标记并使用URLEncoder对其进行编码。

using an XML/HTML parser and getting the text within the two tags is easy. 使用XML / HTML解析器并在两个标签中获取文本很容易。

i got it here 我在这里

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 . 您可以在escapeHtml3escapeHtml4之间进行选择。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM