[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/ -> html2text.php (summary)

(no description)

File Size: 750 lines (26 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

html2text:: (16 methods):
  html2text()
  set_html()
  get_text()
  print_text()
  p()
  set_allowed_tags()
  set_base_url()
  _convert()
  _converter()
  _build_link_list()
  _convert_pre()
  _convert_blockquotes()
  _preg_callback()
  _preg_pre_callback()
  _toupper()
  _strtoupper()


Class: html2text  - X-Ref

Takes HTML and converts it to formatted, plain text.

Thanks to Alexander Krug (http://www.krugar.de/) to pointing out and
correcting an error in the regexp search array. Fixed 7/30/03.

Updated set_html() function's file reading mechanism, 9/25/03.

Thanks to Joss Sanglier (http://www.dancingbear.co.uk/) for adding
several more HTML entity codes to the $search and $replace arrays.
Updated 11/7/03.

Thanks to Darius Kasperavicius (http://www.dar.dar.lt/) for
suggesting the addition of $allowed_tags and its supporting function
(which I slightly modified). Updated 3/12/04.

Thanks to Justin Dearing for pointing out that a replacement for the
<TH> tag was missing, and suggesting an appropriate fix.
Updated 8/25/04.

Thanks to Mathieu Collas (http://www.myefarm.com/) for finding a
display/formatting bug in the _build_link_list() function: email
readers would show the left bracket and number ("[1") as part of the
rendered email address.
Updated 12/16/04.

Thanks to Wojciech Bajon (http://histeria.pl/) for submitting code
to handle relative links, which I hadn't considered. I modified his
code a bit to handle normal HTTP links and MAILTO links. Also for
suggesting three additional HTML entity codes to search for.
Updated 03/02/05.

Thanks to Jacob Chandler for pointing out another link condition
for the _build_link_list() function: "https".
Updated 04/06/05.

Thanks to Marc Bertrand (http://www.dresdensky.com/) for
suggesting a revision to the word wrapping functionality; if you
specify a $width of 0 or less, word wrapping will be ignored.
Updated 11/02/06.

*** Big housecleaning updates below:

Thanks to Colin Brown (http://www.sparkdriver.co.uk/) for
suggesting the fix to handle </li> and blank lines (whitespace).
Christian Basedau (http://www.movetheweb.de/) also suggested the
blank lines fix.

Special thanks to Marcus Bointon (http://www.synchromedia.co.uk/),
Christian Basedau, Norbert Laposa (http://ln5.co.uk/),
Bas van de Weijer, and Marijn van Butselaar
for pointing out my glaring error in the <th> handling. Marcus also
supplied a host of fixes.

Thanks to Jeffrey Silverman (http://www.newtnotes.com/) for pointing
out that extra spaces should be compressed--a problem addressed with
Marcus Bointon's fixes but that I had not yet incorporated.

Thanks to Daniel Schledermann (http://www.typoconsult.dk/) for
suggesting a valuable fix with <a> tag handling.

Thanks to Wojciech Bajon (again!) for suggesting fixes and additions,
including the <a> tag handling that Daniel Schledermann pointed
out but that I had not yet incorporated. I haven't (yet)
incorporated all of Wojciech's changes, though I may at some
future time.

*** End of the housecleaning updates. Updated 08/08/07.

html2text( $source = '', $from_file = false, $do_links = true, $width = 75 )   X-Ref
Constructor.

If the HTML source string (or file) is supplied, the class
will instantiate with that source propagated, all that has
to be done it to call get_text().

param: string $source HTML content
param: boolean $from_file Indicates $source is a file to pull content from
param: boolean $do_links Indicate whether a table of link URLs is desired
param: integer $width Maximum width of the formatted text, 0 for no limit
return: void

set_html( $source, $from_file = false )   X-Ref
Loads source HTML into memory, either from $source string or a file.

param: string $source HTML content
param: boolean $from_file Indicates $source is a file to pull content from
return: void

get_text()   X-Ref
Returns the text, converted from HTML.

return: string

print_text()   X-Ref
Prints the text, converted from HTML.

return: void

p()   X-Ref
Alias to print_text(), operates identically.

return: void

set_allowed_tags( $allowed_tags = '' )   X-Ref
Sets the allowed HTML tags to pass through to the resulting text.

Tags should be in the form "<p>", with no corresponding closing tag.

return: void

set_base_url( $url = '' )   X-Ref
Sets a base URL to handle relative links.

return: void

_convert()   X-Ref
Workhorse function that does actual conversion (calls _converter() method).

return: void

_converter(&$text)   X-Ref
Workhorse function that does actual conversion.

First performs custom tag replacement specified by $search and
$replace arrays. Then strips any remaining HTML tags, reduces whitespace
and newlines to a readable format, and word wraps the text to
$width characters.

param: string Reference to HTML content string
return: void

_build_link_list( $link, $display )   X-Ref
Helper function called by preg_replace() on link replacement.

Maintains an internal list of links to be displayed at the end of the
text, with numeric indices to the original point in the text they
appeared. Also makes an effort at identifying and handling absolute
and relative links.

param: string $link URL of the link
param: string $display Part of the text to associate number with
return: string

_convert_pre(&$text)   X-Ref
Helper function for PRE body conversion.

param: string HTML content

_convert_blockquotes(&$text)   X-Ref
Helper function for BLOCKQUOTE body conversion.

param: string HTML content

_preg_callback($matches)   X-Ref
Callback function for preg_replace_callback use.

param: array PREG matches
return: string

_preg_pre_callback($matches)   X-Ref
Callback function for preg_replace_callback use in PRE content handler.

param: array PREG matches
return: string

_toupper($str)   X-Ref
Strtoupper function with HTML tags and entities handling.

param: string $str Text to convert
return: string Converted text

_strtoupper($str)   X-Ref
Strtoupper multibyte wrapper function with HTML entities handling.

param: string $str Text to convert
return: string Converted text



Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1