HtmlString Class Reference
[Lib]
A class that introduces string functions to html string (that are strings containing tags). More...
Public Member Functions |
|
__construct ($text= '', $policy=self::USE_STRING_FUNCTIONS) | |
build () | |
Create the string. |
|
insert ($text, $pos, $tags_to_skip= '') | |
Insert some html or text, but take care of
tags. |
|
preg_replace ($regex, $replace, $max=-1, $tags_to_skip= '') | |
Do a preg_replace on text. |
|
set_text ($text) | |
Public Attributes |
|
const | USE_PHP_FUNCTIONS = 1 |
const | USE_STRING_FUNCTIONS = 0 |
Protected Member Functions |
|
get_plain_tagname ($tag) | |
Returns the plain text name, e.g. |
|
is_within_tags ($arr_tags, $text_index) | |
Check if a given text block is within given
tags. |
|
rebuild () | |
Rebuld the text and tags arrays. |
Detailed Description
A class that introduces string functions to html string (that are strings containing tags).
Definition at line 8 of file htmlstring.cls.php.
Constructor & Destructor Documentation
HtmlString::__construct | ( | $ | text = '' , |
|
$ | policy =
self::USE_STRING_FUNCTIONS |
|||
) |
Definition at line 16 of file htmlstring.cls.php.
00016 { 00017 $this->set_text($text); 00018 $this->policy = $policy; 00019 }
Member Function Documentation
HtmlString::build | ( | ) |
Create the string.
Definition at line 41 of file htmlstring.cls.php.
00041 { 00042 $count_texts = count($this->html_texts); 00043 $count_tags = count($this->html_tags); 00044 00045 $ret = ''; 00046 // We want to replace text within tags, but not within <a> anchors! 00047 for($index = 0; $index < $count_texts; $index ++) { 00048 $ret .= $this->html_texts[$index]; 00049 // Now add tag, if any 00050 if ($index < $count_tags) { 00051 $ret .= $this->html_tags[$index]; 00052 } 00053 } 00054 00055 return trim($ret); 00056 }
HtmlString::get_plain_tagname | ( | $ | tag | ) | [protected] |
Returns the plain text name, e.g.
Definition at line 200 of file htmlstring.cls.php.
00200 { 00201 $ret = str_replace(array('<', '>', '/'), '', $tag); 00202 $ret = trim($ret); 00203 $ret = String::extract_before($ret, ' '); 00204 return $ret; 00205 }
HtmlString::insert | ( | $ | text, | |
$ | pos, | |||
$ | tags_to_skip = '' |
|||
) |
Insert some html or text, but take care of tags.
Definition at line 152 of file htmlstring.cls.php.
00152 { 00153 $count_texts = count($this->html_texts); 00154 if (!is_array($tags_to_skip)) { 00155 $tags_to_skip = empty($tags_to_skip) ? array() : explode(' ', $tags_to_skip); 00156 } 00157 00158 $arr_startpos = array(); 00159 $arr_endpos = array(); 00160 $pos_total = 0; 00161 // Step though all text blocks... 00162 for($index = 0; $index < $count_texts; $index ++) { 00163 $arr_startpos[] = $pos_total; 00164 $pos_total += String::length($this->html_texts[$index]); 00165 $arr_endpos[] = $pos_total; 00166 00167 if ($pos_total >= $pos) { 00168 $index++; // increment so no match and match can be treated the same 00169 break; 00170 } 00171 } 00172 // We now have index of block where replacement may happen 00173 $index--; 00174 // Step back to see if within forbidden tags 00175 $matching_index = 0; 00176 for (; $index >= 0; $index--) { 00177 if (!$this->is_within_tags($tags_to_skip, $index)) { 00178 $matching_index = $index; 00179 break; 00180 } 00181 } 00182 // We now have index of block where replacement should happen 00183 $block_text = $this->html_texts[$matching_index]; 00184 if ($arr_endpos[$matching_index] < $pos) { 00185 // We have block before wanted pos. Put at end; 00186 $block_text .= $text; 00187 } 00188 else { 00189 // Need to insert it in between. 00190 $tmp = String::substr_word($block_text, 0, $pos - $arr_startpos[$matching_index]); 00191 $block_text = $tmp . $text . String::substr($block_text, String::length($tmp)); 00192 } 00193 $this->html_texts[$matching_index] = $block_text; 00194 $this->rebuild(); 00195 }
HtmlString::is_within_tags | ( | $ | arr_tags, | |
$ | text_index | |||
) | [protected] |
Check if a given text block is within given tags.
Definition at line 109 of file htmlstring.cls.php.
00109 { 00110 if (count($arr_tags) == 0) { 00111 return false; 00112 } 00113 $ret = false; 00114 $stack = array(); 00115 // Loop through all tags before this text block 00116 for($i = $text_index - 1; $i >= 0; $i--) { 00117 $tag = $this->html_tags[$i]; 00118 // Validate tags 00119 if (substr($tag, -2, 2) == '/>') { 00120 // Self closing tag: skip 00121 continue; 00122 } 00123 else if (substr($tag, 0, 2) == '</') { 00124 // Closing tag, push on stack 00125 $tag = $this->get_plain_tagname($tag); 00126 array_unshift($stack, $tag); 00127 } 00128 else { 00129 // Opening tag 00130 $tag = $this->get_plain_tagname($tag); 00131 if (count($stack) > 0 && $stack[0] == $tag) { 00132 // We found opening tag for prior closing tag 00133 array_shift($stack); 00134 continue; 00135 } 00136 else { 00137 // We have an opening tag, without closing tag... 00138 if (in_array($tag, $arr_tags)) { 00139 // Found! 00140 $ret = true; 00141 break; 00142 } 00143 } 00144 } 00145 } 00146 return $ret; 00147 }
HtmlString::preg_replace | ( | $ | regex, | |
$ | replace, | |||
$ | max = -1 , |
|||
$ | tags_to_skip = '' |
|||
) |
Do a preg_replace on text.
- Returns:
- int Number of replacements done
Definition at line 70 of file htmlstring.cls.php.
00070 { 00071 if ($max == 0) { 00072 return 0; 00073 } 00074 00075 $count_texts = count($this->html_texts); 00076 if (!is_array($tags_to_skip)) { 00077 $tags_to_skip = empty($tags_to_skip) ? array() : explode(' ', $tags_to_skip); 00078 } 00079 00080 $num_matches = 0; 00081 // SAtep though all text blocks... 00082 for($index = 0; $index < $count_texts; $index ++) { 00083 if (!$this->is_within_tags($tags_to_skip, $index)) { 00084 $count_matches = 0; 00085 if ($this->policy == self::USE_STRING_FUNCTIONS) { 00086 $this->html_texts[$index] = String::preg_replace($regex, $replace, $this->html_texts[$index], $max, $count_matches); 00087 } 00088 else { 00089 $this->html_texts[$index] = preg_replace($regex, $replace, $this->html_texts[$index], $max, $count_matches); 00090 } 00091 $num_matches += $count_matches; 00092 } 00093 00094 if ($max > 0 && $num_matches >= $max ) { 00095 break; // We did all replacements 00096 } 00097 } 00098 00099 if ($num_matches > 0) { 00100 // We changed the text, so text elements now may contain html. Joind ad resplit text 00101 $this->rebuild(); 00102 } 00103 return $num_matches; 00104 }
HtmlString::rebuild | ( | ) | [protected] |
Rebuld the text and tags arrays.
Definition at line 61 of file htmlstring.cls.php.
HtmlString::set_text | ( | $ | text | ) |
Definition at line 21 of file htmlstring.cls.php.
00021 { 00022 $tag = '#<[^>]*>#'; 00023 $text = ' ' . $text; // We need a leading non-tag, see below 00024 00025 // Split on tags 00026 $this->html_texts = String::preg_split($tag, $text, -1); 00027 00028 // Find all tags 00029 $html_tags = ''; 00030 String::preg_match_all($tag, $text, $html_tags); 00031 $this->html_tags = $html_tags[0]; 00032 00033 // we now have two arrays, one for text between tags ($html_texts), 00034 // and one for the tags itself ($html_tags). When rebuilding the text, 00035 // the first item always is a html text, because we prependend ' '. 00036 }
Member Data Documentation
const HtmlString::USE_PHP_FUNCTIONS = 1 |
Definition at line 10 of file htmlstring.cls.php.
const HtmlString::USE_STRING_FUNCTIONS = 0 |
Definition at line 9 of file htmlstring.cls.php.
The documentation for this class was generated from the following file:
- gyro/core/lib/helpers/htmlstring.cls.php