Ruby 4.0 リファレンスマニュアル

class CGI

[edit]

要約

目次

特異メソッド
定数

特異メソッド

escape(string) -> String[permalink][rdoc][edit]

与えられた文字列を URL エンコードした文字列を新しく作成し返します。

[PARAM] string:
URL エンコードしたい文字列を指定します。
require "cgi"

p CGI.escape('@##')   # => "%40%23%23"

url = "http://www.example.com/register?url=" + 
  CGI.escape('http://www.example.com/index.rss')
p url
# => "http://www.example.com/register?url=http%3A%2F%2Fwww.example.com%2Findex.rss"
escapeElement(string, *elements) -> String[permalink][rdoc][edit]
escape_element(string, *elements) -> StringRuby 1.9.3 から

第二引数以降に指定したエレメントのタグだけを実体参照に置換します。

[PARAM] string:
文字列を指定します。
[PARAM] elements:
HTML タグの名前を一つ以上指定します。文字列の配列で指定することも出来ます。
require "cgi"

p CGI.escapeElement('<BR><A HREF="url"></A>', "A", "IMG")
     # => "<BR>&lt;A HREF="url"&gt;&lt;/A&gt"

p CGI.escapeElement('<BR><A HREF="url"></A>', ["A", "IMG"])
     # => "<BR>&lt;A HREF="url"&gt;&lt;/A&gt"
escapeHTML(string) -> String[permalink][rdoc][edit]
escape_html(string) -> StringRuby 1.9.3 から

与えられた文字列中の '、&、"、<、> を実体参照に置換した文字列を新しく作成し返します。

[PARAM] string:
文字列を指定します。
require "cgi"

p CGI.escapeHTML("3 > 1")   # => "3 &gt; 1"

print('<script type="text/javascript">alert("警告")</script>')

p CGI.escapeHTML('<script type="text/javascript">alert("警告")</script>')
# => "&lt;script type=&quot;text/javascript&quot;&gt;alert(&quot;警告&quot;)&lt;/script&gt;"
escapeURIComponent(string) -> StringRuby 3.2 から[permalink][rdoc][edit]

与えられた文字列を [RFC3986] に従って URL エンコードした文字列を新しく作成し返します。

CGI.escape と異なり、空白文字を + ではなく %20 に変換します。 JavaScript の encodeURIComponent 関数 と同じ結果になります。

[PARAM] string:
URL エンコードしたい文字列を指定します。
require "cgi"

p CGI.escapeURIComponent("'Stop!' said Fred") # => "%27Stop%21%27%20said%20Fred"

# CGI.escape は空白を + にするが、escapeURIComponent は %20 にする
p CGI.escape("a b")             # => "a+b"
p CGI.escapeURIComponent("a b") # => "a%20b"

[SEE_ALSO] CGI.escape, CGI.unescapeURIComponent

unescape(string) -> String[permalink][rdoc][edit]

与えられた文字列を URL デコードした文字列を新しく作成し返します。

[PARAM] string:
URL エンコードされている文字列を指定します。
require "cgi"

p CGI.unescape('%40%23%23')   # => "@##"

p CGI.unescape("http%3A%2F%2Fwww.example.com%2Findex.rss")
# => "http://www.example.com/index.rss"
unescapeElement(string, *elements) -> String[permalink][rdoc][edit]
unescape_element(string, *elements) -> StringRuby 1.9.3 から

特定の要素だけをHTMLエスケープから戻す。

[PARAM] string:
文字列を指定します。
[PARAM] elements:
HTML タグの名前を一つ以上指定します。文字列の配列で指定することも出来ます。
require "cgi"

print CGI.unescapeElement('&lt;BR&gt;&lt;A HREF="url"&gt;&lt;/A&gt;', "A", "IMG")
  # => "&lt;BR&gt;<A HREF="url"></A>"

print CGI.unescapeElement('&lt;BR&gt;&lt;A HREF="url"&gt;&lt;/A&gt;', %w(A IMG))
  # => "&lt;BR&gt;<A HREF="url"></A>"
unescapeHTML(string) -> String[permalink][rdoc][edit]
unescape_html(string) -> StringRuby 1.9.3 から

与えられた文字列中の実体参照のうち、&amp; &gt; &lt; &quot; と数値指定がされているもの (&#0ffff など) を元の文字列に置換します。

[PARAM] string:
文字列を指定します。
require "cgi"

p CGI.unescapeHTML("3 &gt; 1")   # => "3 > 1"
unescapeURIComponent(string) -> StringRuby 3.2 から[permalink][rdoc][edit]

与えられた文字列を CGI.escapeURIComponent でエンコードされたものとして URL デコードした文字列を新しく作成し返します。

[PARAM] string:
URL エンコードされている文字列を指定します。
require "cgi"

p CGI.unescapeURIComponent("%27Stop%21%27%20said%20Fred") # => "'Stop!' said Fred"

[SEE_ALSO] CGI.unescape, CGI.escapeURIComponent

定数

RFC822_DAYS -> [String][permalink][rdoc][edit]

[RFC822] で定義されている曜日の略称を返します。

[SEE_ALSO] [RFC822]

RFC822_MONTHS -> [String][permalink][rdoc][edit]

[RFC822] で定義されている月名の略称を返します。

[SEE_ALSO] [RFC822]

TABLE_FOR_ESCAPE_HTML__ -> HashRuby 1.9.3 から[permalink][rdoc][edit]

HTML 上でエスケープする文字列の変換テーブルを返します。

{
  "'" => '&#39;',
  '&' => '&amp;',
  '"' => '&quot;',
  '<' => '&lt;',
  '>' => '&gt;',
}