百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
H

htmlentities

> 编程语言
开源

HTMLEntities 是一个简单的库,用于方便在 HTML 和 XHTML 中对命名的 (&yacute; 等) 或数值 (&#123; 或 &#x12a;) 实体的编码和解码。

348 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

HTMLEntities 是一个简单的库,用于方便在 HTML 和 XHTML 中对命名的 (&yacute; 等) 或数值 (&#123; 或 &#x12a;) 实体的编码和解码。

HTMLEntities

The canonical source for this project can be found at GitHub: threedaymonk/htmlentities.

HTML entity encoding and decoding for Ruby

HTMLEntities is a simple library to facilitate encoding and decoding of named (&yacute; and so on) or numerical (&#123; or &#x12a;) entities in HTML and XHTML documents.

Usage

Decoding

require 'htmlentities'
coder = HTMLEntities.new
string = "&eacute;lan"
coder.decode(string) # => "élan"

Encoding

This is slightly more complicated, due to the various options. The encode method takes a variable number of parameters, which tell it which instructions to carry out.

require 'htmlentities'
coder = HTMLEntities.new
string = ""

Escape unsafe codepoints only:

coder.encode(string) # => "&lt;élan&gt;"

Or:

coder.encode(string, :basic) # => "&lt;élan&gt;"

Escape all entities that have names:

coder.encode(string, :named) # => "&lt;&eacute;lan&gt;"

Escape all non-ASCII/non-safe codepoints using decimal entities:

coder.encode(string, :decimal) # => "&#60;&#233;lan&#62;"

As above, using hexadecimal entities:

coder.encode(string, :hexadecimal) # => "&#x3c;&#xe9;lan&#x3e;"

You can also use several options, e.g. use named entities for unsafe codepoints, then decimal for all other non-ASCII:

coder.encode(string, :basic, :decimal) # => "&lt;&#233;lan&gt;"

Flavours

HTMLEntities knows about three different sets of entities:

  • :xhtml1 – Entities from the XHTML1 doctype
  • :html4 – Entities from the HTML4 doctype. Differs from +xhtml1+ only by the absence of +&apos+
  • :expanded – Entities from a variety of SGML sets

The default is :xhtml, but you can override this:

coder = HTMLEntities.new(:expanded)

Licence

This code is free to use under the terms of the MIT licence. See the file COPYING.txt for more details.

Contact

Send email to [email protected].

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言