28 $out = ob_get_contents();
42 public static function cropString($text, $length=100, $suffix=
'…', $isHTML=
true) {
44 $simpleTags=array(
'br'=>
true,
'hr'=>
true,
'input'=>
true,
'image'=>
true,
'link'=>
true,
'meta'=>
true);
47 preg_match_all(
'/<[^>]+>([^<]*)/', $text, $m, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
49 if($o[0][1] - $i >= $length) {
52 $t = substr(strtok($o[0][0],
" \t\n\r\0\x0B>"), 1);
54 if($t[0] !=
'/' && (!isset($simpleTags[$t]))) {
57 elseif(end($tags) == substr($t, 1)) {
60 $i += $o[1][1] - $o[0][1];
64 $output = substr($text, 0, $length = min(strlen($text), $length + $i));
66 $output2 = (count($tags = array_reverse($tags)) ?
'' :
'');
68 $pos = @(int)end(end(preg_split(
'/<.*>| /', $output, -1, PREG_SPLIT_OFFSET_CAPTURE)));
72 $one = substr($output, 0, $pos);
74 $two = substr($output, $pos, (strlen($output) - $pos));
76 preg_match_all(
'/<(.*?)>/s', $two, $tags);
78 if (strlen($text) > $length) {
82 $output = $one . implode($tags[0]);
84 $output = str_replace(
'',
'',$output);
95 public static function excerpt($string, $phrase, $radius = 100) {
96 if ($radius > strlen($string)) {
99 $phraseLen = strlen($phrase);
100 if ($radius < $phraseLen) {
101 $radius = $phraseLen;
103 $pos = strpos(strtolower($string), strtolower($phrase));
106 if ($pos > $radius) {
107 $startPos = $pos - $radius;
109 $textLen = strlen($string);
111 $endPos = $pos + $phraseLen + $radius;
112 if ($endPos >= $textLen) {
117 $firstSpacePos = strpos($string,
" ", $startPos);
118 $lastSpacePos = strrpos($string,
" ", -(strlen($string)-$endPos));
120 $excerpt1 = substr($string, $firstSpacePos, $lastSpacePos-$firstSpacePos);
123 $excerpt = preg_replace(
'/^[^<]*?>|<[^>]*?$/',
'', $excerpt1);
136 preg_match_all(
"/<a[^>]+href=\"([^\">]+)/i", $string, $links);
139 for ($i=0; $i<
sizeof($links[1]); $i++) {
140 if (preg_match_all(
"/javascript:.*window.open[\(]*'([^']+)/i", $links[1][$i], $popups)) {
141 $links[1][$i] = $popups[1][0];
145 for ($i=0; $i<
sizeof($links[1]); $i++) {
146 if (preg_match(
"/^mailto:/i", $links[1][$i])) {
147 unset($links[1][$i]);
150 preg_match_all(
"/<img[^>]+src=\"([^\">]+)/i", $string, $images);
151 preg_match_all(
"/<input[^>]+src=\"([^\">]+)/i", $string, $buttons);
152 preg_match_all(
"/<form[^>]+action=\"([^\">]+)/i", $string, $actions);
153 preg_match_all(
"/<link[^>]+href=\"([^\">]+)/i", $string, $css);
154 preg_match_all(
"/<script[^>]+src=\"([^\">]+)/i", $string, $scripts);
155 return array_merge($links[1], $images[1], $buttons[1], $actions[1], $css[1], $scripts[1]);
176 $l = strlen($string);
178 while (($p < $l) && (strpos(
" \r\t\n",$string[$p]) !==
false)) {
181 if ($string[$p] ==
'"') {
184 while (($p < $l) && ($string[$p] !=
'"')) {
185 if ($string[$p] ==
'\\') {
191 $r[] = stripslashes(substr($string, $q, $p-$q));
193 while (($p < $l) && (strpos(
" \r\t\n",$string[$p]) !==
false)) {
198 else if ($string[$p] ==
"'") {
201 while (($p < $l) && ($string[$p] !=
"'")) {
202 if ($string[$p] ==
'\\') {
208 $r[] = stripslashes(substr($string, $q, $p-$q));
210 while (($p < $l) && (strpos(
" \r\t\n",$string[$p]) !==
false)) {
217 while (($p < $l) && (strpos(
",;",$string[$p]) ===
false)) {
220 $r[] = stripslashes(trim(substr($string, $q, $p-$q)));
221 while (($p < $l) && (strpos(
" \r\t\n",$string[$p]) !==
false)) {
239 public static function splitQuoted($string, $delim=
'/ /', $quoteChr=
'"', $preserve=
false){
242 $expEncArr = explode($quoteChr, $string);
243 foreach($expEncArr as $encItem) {
245 $resArr[] = array_pop($resArr) . ($preserve?$quoteChr:
'') . $encItem.($preserve?$quoteChr:
'');
248 $expDelArr = preg_split($delim, $encItem);
249 $resArr[] = array_pop($resArr) . array_shift($expDelArr);
250 $resArr = array_merge($resArr, $expDelArr);
264 if (is_string($string)) {
265 $str = str_replace(
' ',
'', ucwords(str_replace(
'_',
' ', $string)));
266 if ($firstLowerCase) {
267 $str{0} = strtolower($str{0});
283 $patterns = array(
'/\//',
'/\^/',
'/\./',
'/\$/',
'/\|/',
'/\(/',
'/\)/',
'/\[/',
'/\]/',
'/\*/',
'/\+/',
'/\?/',
'/\{/',
'/\}/');
284 $replace = array(
'\/',
'\^',
'\.',
'\$',
'\|',
'\(',
'\)',
'\[',
'\]',
'\*',
'\+',
'\?',
'\{',
'\}');
286 return preg_replace($patterns, $replace, $string);
295 return preg_replace(
'/, ?$/',
'', $string);
304 $val = filter_var($string, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
317 public static function slug($string) {
318 $search = array(
'Ä',
'Ö',
'Ü',
'ä',
'ö',
'ü',
'ß');
319 $replace = array(
'AE',
'OE',
'UE',
'ae',
'oe',
'ue',
'ss');
320 $string = str_replace($search, $replace, $string);
321 return strtolower(trim(preg_replace(
'~[^0-9a-z]+~i',
'-',
322 html_entity_decode(preg_replace(
'~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i',
'$1',
323 htmlentities($string, ENT_QUOTES,
'UTF-8')), ENT_QUOTES,
'UTF-8')),
'-'));
static excerpt($string, $phrase, $radius=100)
Create an excerpt from the given text around the given phrase code based on: http://stackoverflow.com/questions/1292121/how-to-generate-the-snippet-like-generated-by-google-with-php-and-mysql.
static removeTrailingComma($string)
Remove a trailing comma, if existing.
StringUtil provides support for string manipulation.
static getBoolean($string)
Get the boolean value of a string.
static cropString($text, $length=100, $suffix='…', $isHTML=true)
Truncate a string up to a number of characters while preserving whole words and HTML tags code based ...
static splitQuoted($string, $delim='//', $quoteChr='"', $preserve=false)
Split string preserving quoted strings code based on: http://www.php.net/manual/en/function.explode.php#94024.
static escapeForRegex($string)
Escape characters of a string for use in a regular expression Code from http://php.net/manual/de/function.preg-replace.php.
static quotesplit($string)
Split a quoted string code from: http://php3.de/manual/de/function.split.php.
static getUrls($string)
Extraxt urls from a string.
static underScoreToCamelCase($string, $firstLowerCase=false)
Convert a string in underscore notation to camel case notation.
static slug($string)
Converts all accent characters to ASCII characters.
static getDump($var)
Get the dump of a variable as string.