HTML utilities
"uweb/html.php"
Uray Web Library (UWeb)
This file contains some functions related to static HTML generation and HTML processing. See also utils.php
(which is included by html.php
) for the most basic HTML functions. Here are the more complex and/or less frequently used ones.
3.3. print_num_list(), gen_num_list()
function code (...$text);
Enclose $text
in <code>
HTML tags, and return the result.
Multiple arguments are concatenated to a single string.
function html_to_text ($code);
Remove the HTML tags in $code
, reduce the spaces, and return the result. All whitespace characters (newline, tab, space) are converted to spaces, and they are removed from the beginning and end of the string.
Example: html_to_text("\t<p> some <b>text</b> </p>\n") == "some text"
function split_html ($text);
Split the HTML code into an array of text segments and tags.
Example: split_html("<p>some <b>text</b></p>") == ["<p>", "some ", "<b>", "text", "</b>", "</p>"]
Note: always implode(split_html($string)) == $string
.
function print_list ($items, $attrs = []); function print_list ($lng, $items, $attrs = []);
Print an HTML bullet list (<ul>
).
$items
: array of list items. Each element can be:
get_lng_text()
, in which case $lng
is mandatory; null
, which is skipped (i.e. no <li>
is produced). $attrs
: optional additional attributes of the <ul>
element (see: attrs()
).
$lng
: optional language object for multi-language texts. It can be null or even omitted.
function gen_list ($items, $attrs = []); function gen_list ($lng, $items, $attrs = []);
Same as print_list()
, but return the list HTML as a string instead of printing it.
function print_num_list ($items, $attrs = []); function print_num_list ($lng, $items, $attrs = []); function gen_num_list ($items, $attrs = []); function gen_num_list ($lng, $items, $attrs = []);
Same as print_list()
and gen_list()
, respectively, but they print/return a numbered list (<ol>
) instead of a bullet list.
function print_list_recursively ($list); function print_list_recursively ($lng, $list);
Print a multi-level HTML bullet list recursively from an array.
$list
: an array containing the list elements as strings or arrays, recursively. It can be in one of the following two forms:
get_lng_text()
(if $lng
is given). $lng
: optional language object for multi-language texts. It can be null or even omitted.
Example: both of the following lines produce the list below:
print_list_recursively(["First item", "Second", ["2A", "2B"], "Third"]); print_list_recursively([["First ","item "], ["Second ", [["2A "], ["2B "]]], ["Third "]]);
function print_toc_content ($obj, $toc, $start = 1);
Generate table of contents (TOC) and the content itself.
For a simple example, see HTML generators. Also, this documentation page was generated using print_toc_content()
, so see its source code (docs/html.php
) for another example.
$obj
: the object whose methods and constants are referenced by $toc
for the section titles and contents.
$toc
: array of sections, each is [$level, $cont]
or [$level, $cont, $id]
, where:
$level
: the level of the heading, more precisely the number of the HTML heading element, e.g. 4 means <h4>
. The numbering of the sections are deduced from these heading levels, e.g. the headings <h3>
, <h5>
, <h3>
will produce the section numbers 1, 1.1 and 2, respectively. $cont
: the method that generates the content of the section, either as a method name ($cont == $method
) of the argument $obj
above, or as an array $cont == [$obj, $method]
. In both cases, $obj->$method()
is called, which should print the content of the section. "{$method}_title"
of the same $obj
is used as the section title. $cont
, i.e. [$obj, $method, $title]
or [$method, $title]
, so that the class constant $obj::$title
is used as the section title. $obj->lng_obj()
is valid, then the title constant is first searched in the object $obj->lng_obj()
(so it can be language-dependent), then in $obj
itself. $method == "toc"
, then this section is actually the table of contents, in which case no method is called (but the title works in the same way, i.e. by the constant toc_title
). This "toc"
should be present in the list, usually at the beginning, otherwise the TOC is not printed! $method
can be preceded by an asterisk ("*"
) to indicate that this section has no number (otherwise see $level
above for how these numbers are generated). It is usually applied to the TOC, i.e. "*toc"
. $id
(optional): the HTML id
of the section, so it can be referred to by the URL xyz.html#id
. By default, $id
is the method name ($method
) of $cont
. $start
: the first section number, by default 1
. (This is useful if the TOC is split into multiple parts.)
function print_toc ($obj, $toc, $start = 1);
Generate table of contents (TOC).
This works similarly to print_toc_content()
, with the following differences:
<h*>
) of the TOC is not printed. "toc"
section is not necessary (it determines only whether the TOC is listed inside the TOC). $obj->$method()
does not need to exist ($method
is only used for the title). $id
member of $toc
can also be a URL (either internal or external) if it contains the character #
(i.e. it can be either "id"
, "#id"
or "other.html#id"
). (This is useful if the TOC and the content are on separate pages.)