1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352:
<?php
class TextHelper
{
const HTML_NO_PROTECT = false;
const HTML_PROTECT = true;
const ADDSLASHES_FORCE = 1;
const ADDSLASHES_NONE = 2;
public static function strprotect($var, $html_protect = self::HTML_PROTECT, $addslashes = self::ADDSLASHES_FORCE)
{
$var = trim((string)$var);
if ($html_protect)
{
$var = self::htmlspecialchars($var);
$var = preg_replace('`&((?:#[0-9]{2,5})|(?:[a-z0-9]{2,8}));`iu', "&$1;", $var);
}
switch ($addslashes)
{
case self::ADDSLASHES_FORCE:
default:
$var = addslashes($var);
break;
case self::ADDSLASHES_NONE:
$var = stripslashes($var);
break;
}
return $var;
}
public static function wordwrap_html($str, $lenght, $cut_char = '<br />', $cut = true)
{
$str = self::utf8_wordwrap(self::html_entity_decode($str), $lenght, $cut_char, $cut);
return str_replace('<br />', '<br />', self::htmlspecialchars($str, ENT_NOQUOTES));
}
public static function substr_html($str, $start, $end = '')
{
if ($end == '')
return self::htmlspecialchars(self::substr(self::html_entity_decode($str), $start), ENT_NOQUOTES);
else
return self::htmlspecialchars(self::substr(self::html_entity_decode($str), $start, $end), ENT_NOQUOTES);
}
public static function cut_string($string, $length)
{
if (strlen($string) <= $length)
return $string;
$str = mb_substr(str_replace('<br />', '<br/>', $string), 0, $length + 1, 'UTF-8');
return substr($str, 0, strrpos($str, ' ')) . '...';
}
public static function to_js_string($string, $add_quotes = true)
{
$bounds = $add_quotes ? '\'' : '';
return $bounds . str_replace(array("\r\n", "\r", "\n", '"'), array('\n', '\n', '\n', '"'), addcslashes($string, '\'')) . $bounds;
}
public static function to_json_string($string, $add_quotes = true)
{
$bounds = $add_quotes ? '"' : '';
return $bounds . str_replace(array("\r\n", "\r", "\n",), array('\n', '\n', '\n',), addcslashes($string, '"')) . $bounds;
}
public static function htmlspecialchars($string, $flags = null, $encoding = 'UTF-8', $double_encode = true)
{
if ($flags === null)
$flags = ENT_COMPAT;
return str_replace('&', '&', htmlspecialchars($string, $flags, $encoding, $double_encode));
}
public static function htmlspecialchars_decode($string, $flags = null)
{
if ($flags === null)
$flags = ENT_COMPAT;
return htmlspecialchars_decode($string, $flags);
}
public static function html_entity_decode($string, $flags = null, $encoding = 'UTF-8')
{
if ($flags === null)
$flags = ENT_COMPAT;
return html_entity_decode($string, $flags, $encoding);
}
public static function strtolower($string)
{
return mb_strtolower($string);
}
public static function strtoupper($string)
{
return mb_strtoupper($string);
}
public static function lcfirst($string)
{
$fc = mb_strtolower(mb_substr($string, 0, 1));
return $fc . mb_substr($string, 1);
}
public static function ucfirst($string)
{
$fc = mb_strtoupper(mb_substr($string, 0, 1));
return $fc . mb_substr($string, 1);
}
public static function strlen($string)
{
return strlen($string);
}
public static function strpos($string, $substring, $offset = '')
{
if (!empty($substring))
{
if (is_int($offset))
return mb_strpos($string, $substring, $offset);
else
return mb_strpos($string, $substring);
}
return false;
}
public static function stripos($string, $substring, $offset = '')
{
if (!empty($substring))
{
if (is_int($offset))
return mb_stripos($string, $substring, $offset);
else
return mb_stripos($string, $substring);
}
return false;
}
public static function substr($string, $start, $length = '')
{
if (is_int($length))
return substr($string, $start, $length);
else
return substr($string, $start);
}
public static function mb_substr($string, $start, $length = '')
{
if (is_int($length))
return mb_substr($string, $start, $length);
else
return mb_substr($string, $start);
}
public static function strrchr($string, $needle)
{
return mb_strrchr($string, $needle);
}
public static function strripos($string, $needle, $offset = '')
{
if (is_int($offset))
return mb_strripos($string, $needle, $offset);
else
return mb_strripos($string, $needle);
}
public static function strrpos($string, $needle, $offset = '')
{
if (is_int($offset))
return mb_strrpos($string, $needle, $offset);
else
return mb_strrpos($string, $needle);
}
public static function strstr($string, $needle, $before_needle = '')
{
if (is_int($before_needle))
return mb_strstr($string, $needle, $before_needle);
else
return mb_strstr($string, $needle);
}
public static function substr_count($string, $needle, $encoding = '')
{
if ($encoding != '')
return mb_substr_count($string, $needle, $encoding);
else
return mb_substr_count($string, $needle);
}
public static function convert_case($string, $mode, $encoding = '')
{
if ($encoding != '')
return mb_convert_case($string, $mode, $encoding);
else
return mb_convert_case($string, $mode);
}
public static function serialize($string)
{
return serialize($string);
}
public static function serialize_base64($string)
{
return base64_encode(self::serialize($string));
}
public static function unserialize($string)
{
return unserialize(self:: is_base64($string) ? base64_decode($string) : $string);
}
public static function mb_unserialize($string)
{
$string = preg_replace_callback('!s:(\d+):"(.*?)";!s', function ($matches) {
if (isset($matches[2])) return 's:' . strlen($matches[2]) . ':"' . $matches[2] . '";';
}, $string
);
return unserialize($string);
}
private static function is_base64($string)
{
$decoded = base64_decode($string, true);
return preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string) && false !== $decoded && base64_encode($decoded) == $string;
}
public static function check_nbr_links($contents, $max_nbr, $has_html_links = false)
{
if ($max_nbr == -1)
return true;
if ($has_html_links)
$nbr_link = preg_match_all('`<a href="(?:ftp|https?)://`u', $contents, $array);
else
$nbr_link = preg_match_all('`(?:ftp|https?)://`u', $contents, $array);
if ($nbr_link !== false && $nbr_link > $max_nbr)
return false;
return true;
}
public static function utf8_wordwrap($string, $width = 75, $break = "\n", $cut = true)
{
$lines = array();
while (!empty($string))
{
if (preg_match('%^(.{1,' . $width . '})(?:\s|$)%u', $string, $matches))
{
$lines[] = $matches[1];
$string = self::substr($string, self::strlen($matches[0]));
}
else
{
$lines[] = self::substr($string, 0, $width);
$string = self::substr($string, $width);
}
}
return implode($break, $lines);
}
}
?>