1HTML::Entities(3) User Contributed Perl Documentation HTML::Entities(3)
2
3
4
6 HTML::Entities - Encode or decode strings with HTML entities
7
9 use HTML::Entities;
10
11 $a = "Våre norske tegn bør æres";
12 decode_entities($a);
13 encode_entities($a, "\200-\377");
14
15 For example, this:
16
17 $input = "vis-à-vis Beyoncé's naïve\npapier-mâché résumé";
18 print encode_entities($input), "\n"
19
20 Prints this out:
21
22 vis-à-vis Beyoncé's naïve
23 papier-mâché résumé
24
26 This module deals with encoding and decoding of strings with HTML char‐
27 acter entities. The module provides the following functions:
28
29 decode_entities( $string, ... )
30 This routine replaces HTML entities found in the $string with the
31 corresponding Unicode character. Under perl 5.6 and earlier only
32 characters in the Latin-1 range are replaced. Unrecognized entities
33 are left alone.
34
35 If multiple strings are provided as argument they are each decoded
36 separately and the same number of strings are returned.
37
38 If called in void context the arguments are decoded in-place.
39
40 This routine is exported by default.
41
42 _decode_entities( $string, \%entity2char )
43 _decode_entities( $string, \%entity2char, $expand_prefix )
44 This will in-place replace HTML entities in $string. The
45 %entity2char hash must be provided. Named entities not found in
46 the %entity2char hash are left alone. Numeric entities are
47 expanded unless their value overflow.
48
49 The keys in %entity2char are the entity names to be expanded and
50 their values are what they should expand into. The values do not
51 have to be single character strings. If a key has ";" as suffix,
52 then occurrences in $string are only expanded if properly termi‐
53 nated with ";". Entities without ";" will be expanded regardless
54 of how they are terminated for compatiblity with how common
55 browsers treat entities in the Latin-1 range.
56
57 If $expand_prefix is TRUE then entities without trailing ";" in
58 %entity2char will even be expanded as a prefix of a longer unrecog‐
59 nized name. The longest matching name in %entity2char will be
60 used. This is mainly present for compatibility with an MSIE misfea‐
61 ture.
62
63 $string = "foo bar";
64 _decode_entities($string, { nb => "@", nbsp => "\xA0" }, 1);
65 print $string; # will print "foo bar"
66
67 This routine is exported by default.
68
69 encode_entities( $string )
70 encode_entities( $string, $unsafe_chars )
71 This routine replaces unsafe characters in $string with their
72 entity representation. A second argument can be given to specify
73 which characters to consider unsafe (i.e., which to escape). The
74 default set of characters to encode are control chars, high-bit
75 chars, and the "<", "&", ">", "'" and """ characters. But this,
76 for example, would encode just the "<", "&", ">", and """ charac‐
77 ters:
78
79 $encoded = encode_entities($input, '<>&"');
80
81 This routine is exported by default.
82
83 encode_entities_numeric( $string )
84 encode_entities_numeric( $string, $unsafe_chars )
85 This routine works just like encode_entities, except that the
86 replacement entities are always "&#xhexnum;" and never "&entname;".
87 For example, "encode_entities("r\xF4le")" returns "rôle", but
88 "encode_entities_numeric("r\xF4le")" returns "rôle".
89
90 This routine is not exported by default. But you can always export
91 it with "use HTML::Entities qw(encode_entities_numeric);" or even
92 "use HTML::Entities qw(:DEFAULT encode_entities_numeric);"
93
94 All these routines modify the string passed as the first argument, if
95 called in a void context. In scalar and array contexts, the encoded or
96 decoded string is returned (without changing the input string).
97
98 If you prefer not to import these routines into your namespace, you can
99 call them as:
100
101 use HTML::Entities ();
102 $decoded = HTML::Entities::decode($a);
103 $encoded = HTML::Entities::encode($a);
104 $encoded = HTML::Entities::encode_numeric($a);
105
106 The module can also export the %char2entity and the %entity2char
107 hashes, which contain the mapping from all characters to the corre‐
108 sponding entities (and vice versa, respectively).
109
111 Copyright 1995-2006 Gisle Aas. All rights reserved.
112
113 This library is free software; you can redistribute it and/or modify it
114 under the same terms as Perl itself.
115
116
117
118perl v5.8.8 2006-04-26 HTML::Entities(3)