.

ConverterHtmlEx Class Reference
[Lib]

Converts plain text to HTML (encode) or HTML to plain text (decode). More...

Inheritance diagram for ConverterHtmlEx:
ConverterHtml IConverter

List of all members.

Public Member Functions

  decode ($value, $params=false)
  Smarter decoding og html into plain text than done by HTML Converter.

Protected Member Functions

  decode_anchors ($value, $link_format)
  Decode a HTML tag into plain text.
  process_paragraph ($text, $params)
  Process a single paragraph.
  relative_to_absolute ($text, $base)
  Helper.

Detailed Description

Converts plain text to HTML (encode) or HTML to plain text (decode).

Treats short paragraphs as headings

Author:
Gerd Riesselmann

Definition at line 12 of file htmlex.converter.php.


Member Function Documentation

ConverterHtmlEx::decode ( value,
params = false  
)

Smarter decoding og html into plain text than done by HTML Converter.

Keeps Links

The following parameters are supported:

Parameters:
array  $params Associative array supporting the following featurs:
  • p: Text after paragraph. Default is "\n"
  • br: Text after a
    tag. Default is "\n"
  • a: Format to decode tags. Supports $title$ and $url$ placeholders. Default is "$title$: $url$"

Reimplemented from ConverterHtml.

Definition at line 42 of file htmlex.converter.php.

00042                                                         {
00043                 // If there is no HTML, decoding would do more harm than good
00044                 if (!preg_match('@<\w+.*?>@', $value)) {
00045                         return $value;
00046                 }
00047                 
00048                 // Turn <a href="xxx">Text</a> into e.g. Text: xxx
00049                 $value = $this->decode_anchors($value, Arr::get_item($params, 'a', '$title$: $url$'));
00050                 
00051                 // Remove paragraphs
00052                 $value = str_replace("\r", "\n", $value);
00053                 $value = String::preg_replace('|\n+|', " ", $value);
00054                 
00055                 // Replace <p> and <br>
00056                 $value = preg_replace('@\s*<p>\s*@', "<p>", $value);
00057                 $after_p = Arr::get_item($params, 'p', "\n");
00058                 $value = preg_replace('@\s*</p>\s*@', "</p>$after_p", $value);
00059                 $after_br = Arr::get_item($params, 'br', "\n");
00060                 $value = preg_replace('@<br.*?>\s*@', $after_br, $value);
00061                 
00062                 // Strip HTML
00063                 $value = strip_tags($value);
00064                 $value = String::unescape($value);
00065                 $value = String::preg_replace('| +|', ' ', $value);
00066                 
00067                 return $value;          
00068         }
ConverterHtmlEx::decode_anchors ( value,
link_format  
) [protected]

Decode a HTML tag into plain text.

Parameters:
string  $value
string  $link_format Supports placeholders $title$ and $url$

Definition at line 76 of file htmlex.converter.php.

00076                                                                 {
00077                 $value = $this->relative_to_absolute($value, Config::get_url(Config::URL_BASEURL));
00078                 $link_format = str_replace('$title$', '$2', $link_format);
00079                 $link_format = str_replace('$url$', '$1', $link_format);
00080                 return String::preg_replace('|<a.*?href="(.*?)".*?>(.*?)</a>|', $link_format, $value);
00081         }
ConverterHtmlEx::process_paragraph ( text,
params  
) [protected]

Process a single paragraph.

Parameters:
string  $text
Returns:
string

Reimplemented from ConverterHtml.

Definition at line 19 of file htmlex.converter.php.

00019                                                              {
00020                 if (String::length($text) <= 70 && String::right($text, 1) != '.') {
00021                         $level = intval(Arr::get_item($params, 'h', 2));
00022                         return html::tag('h' . $level, $text);  
00023                 }
00024                 else {
00025                         return parent::process_paragraph($text, $params);
00026                 }
00027         }
ConverterHtmlEx::relative_to_absolute ( text,
base  
) [protected]

Helper.

Turn relative URLs in text to an absolute one

Parameters:
string  $text Text containing URLs
string  $base baseurl

Definition at line 89 of file htmlex.converter.php.

00089                                                               {
00090                 if (empty($base)) {
00091                         return $text;
00092                 }
00093                         
00094                 if (substr($base, -1, 1) != "/") { $base .= "/"; }
00095                 $domain = Url::create($base)->clear_query()->set_path('')->build(Url::ABSOLUTE);
00096                 
00097                 // Replace href="/abc" with domain
00098                 $pattern =      '|<a(.*?) href="/(.*?)"|';
00099                 $replace = '<a$1 href="' . $domain . '$2"';
00100                 $text = preg_replace($pattern, $replace, $text);
00101                 
00102                 // Replace href="abc" with base
00103                 $pattern =      '|<a(.*?) href="(?!\w+://)(.*?)"|';
00104                 $replace = '<a$1 href="' . $base . '$2"';
00105                 $text = preg_replace($pattern, $replace, $text);
00106                 
00107                 return $text;
00108         }       

The documentation for this class was generated from the following file: