38 $out = ob_get_contents();
52 function cropString($text, $length=100, $ending=
'...', $exact=
false, $considerHtml=
true)
56 if (strlen(preg_replace(
'/<.*?>/',
'', $text)) <= $length) {
60 preg_match_all(
'/(<.+?>)?([^<>]*)/s', $text, $lines, PREG_SET_ORDER);
61 $total_length = strlen($ending);
64 foreach ($lines as $line_matchings) {
66 if (!empty($line_matchings[1])) {
68 if (preg_match(
'/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1])) {
71 }
else if (preg_match(
'/^<\s*\/([^\s]+?)\s*>$/s', $line_matchings[1], $tag_matchings)) {
73 $pos = array_search($tag_matchings[1], $open_tags);
75 unset($open_tags[$pos]);
78 }
else if (preg_match(
'/^<\s*([^\s>!]+).*?>$/s', $line_matchings[1], $tag_matchings)) {
80 array_unshift($open_tags, strtolower($tag_matchings[1]));
83 $truncate .= $line_matchings[1];
86 $content_length = strlen(preg_replace(
'/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i',
' ', $line_matchings[2]));
87 if ($total_length+$content_length> $length) {
89 $left = $length - $total_length;
92 if (preg_match_all(
'/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', $line_matchings[2], $entities, PREG_OFFSET_CAPTURE)) {
94 foreach ($entities[0] as $entity) {
95 if ($entity[1]+1-$entities_length <= $left) {
97 $entities_length += strlen($entity[0]);
104 $truncate .= substr($line_matchings[2], 0, $left+$entities_length);
108 $truncate .= $line_matchings[2];
109 $total_length += $content_length;
112 if($total_length>= $length) {
117 if (strlen($text) <= $length) {
120 $truncate = substr($text, 0, $length - strlen($ending));
126 $spacepos = strrpos($truncate,
' ');
127 if (isset($spacepos)) {
129 $truncate = substr($truncate, 0, $spacepos);
133 $truncate .= $ending;
136 foreach ($open_tags as $tag) {
137 $truncate .=
'</' . $tag .
'>';
149 return preg_replace(
'/, ?$/',
'', $string);
160 preg_match_all(
"/<a[^>]+href=\"([^\">]+)/i", $string, $links);
163 for ($i=0; $i<
sizeof($links[1]); $i++)
164 if (preg_match_all(
"/javascript:.*window.open[\(]*'([^']+)/i", $links[1][$i], $popups))
165 $links[1][$i] = $popups[1][0];
168 for ($i=0; $i<
sizeof($links[1]); $i++)
169 if (preg_match(
"/^mailto:/i", $links[1][$i]))
170 unset($links[1][$i]);
172 preg_match_all(
"/<img[^>]+src=\"([^\">]+)/i", $string, $images);
173 preg_match_all(
"/<input[^>]+src=\"([^\">]+)/i", $string, $buttons);
174 preg_match_all(
"/<form[^>]+action=\"([^\">]+)/i", $string, $actions);
175 return array_merge($links[1], $images[1], $buttons[1], $actions[1]);
199 while (($p < $l) && (strpos(
" \r\t\n",$s[$p]) !==
false)) $p++;
204 while (($p < $l) && ($s[$p] !=
'"'))
206 if ($s[$p] ==
'\\') { $p+=2;
continue; }
209 $r[] = stripslashes(substr($s, $q, $p-$q));
211 while (($p < $l) && (strpos(
" \r\t\n",$s[$p]) !==
false)) $p++;
214 else if ($s[$p] ==
"'")
218 while (($p < $l) && ($s[$p] !=
"'"))
220 if ($s[$p] ==
'\\') { $p+=2;
continue; }
223 $r[] = stripslashes(substr($s, $q, $p-$q));
225 while (($p < $l) && (strpos(
" \r\t\n",$s[$p]) !==
false)) $p++;
231 while (($p < $l) && (strpos(
",;",$s[$p]) ===
false))
235 $r[] = stripslashes(trim(substr($s, $q, $p-$q)));
236 while (($p < $l) && (strpos(
" \r\t\n",$s[$p]) !==
false)) $p++;
251 function splitQuoted($str, $delim=
'/ /', $quoteChr=
'"', $preserve=
false) {
254 $expEncArr = explode($quoteChr, $str);
255 foreach($expEncArr as $encItem) {
257 array_push($resArr, array_pop($resArr) . ($preserve?$quoteChr:
'') . $encItem.($preserve?$quoteChr:
''));
260 $expDelArr = preg_split($delim, $encItem);
261 array_push($resArr, array_pop($resArr) . array_shift($expDelArr));
262 $resArr = array_merge($resArr, $expDelArr);
271 function excerpt($text, $phrase, $radius = 100) {
272 $phraseLen = strlen($phrase);
273 if ($radius < $phraseLen) {
274 $radius = $phraseLen;
276 $pos = strpos(strtolower($text), strtolower($phrase));
279 if ($pos > $radius) {
280 $startPos = $pos - $radius;
282 $textLen = strlen($text);
284 $endPos = $pos + $phraseLen + $radius;
285 if ($endPos >= $textLen) {
290 $firstSpacePos = strpos($text,
" ", $startPos);
291 $lastSpacePos = strrpos($text,
" ", -(strlen($text)-$endPos));
293 $excerpt1 = substr($text, $firstSpacePos, $lastSpacePos-$firstSpacePos);
296 $excerpt = preg_replace(
'/^[^<]*?>|<[^>]*?$/',
'', $excerpt1);
cropString($text, $length=100, $ending='...', $exact=false, $considerHtml=true)
removeTrailingComma($string)
StringUtil provides support for string manipulation.
splitQuoted($str, $delim='//', $quoteChr='"', $preserve=false)
excerpt($text, $phrase, $radius=100)