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
27 character 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. Unrecognized entities are left
32 alone.
33
34 If multiple strings are provided as argument they are each decoded
35 separately and the same number of strings are returned.
36
37 If called in void context the arguments are decoded in-place.
38
39 This routine is exported by default.
40
41 _decode_entities( $string, \%entity2char )
42 _decode_entities( $string, \%entity2char, $expand_prefix )
43 This will in-place replace HTML entities in $string. The
44 %entity2char hash must be provided. Named entities not found in
45 the %entity2char hash are left alone. Numeric entities are
46 expanded unless their value overflow.
47
48 The keys in %entity2char are the entity names to be expanded and
49 their values are what they should expand into. The values do not
50 have to be single character strings. If a key has ";" as suffix,
51 then occurrences in $string are only expanded if properly
52 terminated with ";". Entities without ";" will be expanded
53 regardless of how they are terminated for compatibility with how
54 common browsers treat entities in the Latin-1 range.
55
56 If $expand_prefix is TRUE then entities without trailing ";" in
57 %entity2char will even be expanded as a prefix of a longer
58 unrecognized name. The longest matching name in %entity2char will
59 be used. This is mainly present for compatibility with an MSIE
60 misfeature.
61
62 $string = "foo bar";
63 _decode_entities($string, { nb => "@", nbsp => "\xA0" }, 1);
64 print $string; # will print "foo bar"
65
66 This routine is exported by default.
67
68 encode_entities( $string )
69 encode_entities( $string, $unsafe_chars )
70 This routine replaces unsafe characters in $string with their
71 entity representation. A second argument can be given to specify
72 which characters to consider unsafe. The unsafe characters is
73 specified using the regular expression character class syntax (what
74 you find within brackets in regular expressions).
75
76 The default set of characters to encode are control chars, high-bit
77 chars, and the "<", "&", ">", "'" and """ characters. But this,
78 for example, would encode just the "<", "&", ">", and """
79 characters:
80
81 $encoded = encode_entities($input, '<>&"');
82
83 and this would only encode non-plain ascii:
84
85 $encoded = encode_entities($input, '^\n\x20-\x25\x27-\x7e');
86
87 This routine is exported by default.
88
89 encode_entities_numeric( $string )
90 encode_entities_numeric( $string, $unsafe_chars )
91 This routine works just like encode_entities, except that the
92 replacement entities are always "&#xhexnum;" and never "&entname;".
93 For example, "encode_entities("r\xF4le")" returns "rôle", but
94 "encode_entities_numeric("r\xF4le")" returns "rôle".
95
96 This routine is not exported by default. But you can always export
97 it with "use HTML::Entities qw(encode_entities_numeric);" or even
98 "use HTML::Entities qw(:DEFAULT encode_entities_numeric);"
99
100 All these routines modify the string passed as the first argument, if
101 called in a void context. In scalar and array contexts, the encoded or
102 decoded string is returned (without changing the input string).
103
104 If you prefer not to import these routines into your namespace, you can
105 call them as:
106
107 use HTML::Entities ();
108 $decoded = HTML::Entities::decode($a);
109 $encoded = HTML::Entities::encode($a);
110 $encoded = HTML::Entities::encode_numeric($a);
111
112 The module can also export the %char2entity and the %entity2char
113 hashes, which contain the mapping from all characters to the
114 corresponding entities (and vice versa, respectively).
115
117 Copyright 1995-2006 Gisle Aas. All rights reserved.
118
119 This library is free software; you can redistribute it and/or modify it
120 under the same terms as Perl itself.
121
122
123
124perl v5.28.1 2013-10-21 HTML::Entities(3)