Show string.php syntax highlighted
<?php
require_once('ext/Text_Diff/Diff.php');
require_once('ext/Text_Diff/Diff/Renderer/inline.php');
/**
* Additional String methods.
*
* @package String
* @author Luke Satin <cyberluk@seznam.cz>
* @version 0.9.6.3
*/
class String {
private static $randomWordsCache = array ();
private static $letters = array (
"a",
"a",
"c",
"d",
"e",
"e",
"i",
"l",
"l",
"n",
"o",
"o",
"o",
"o",
"r",
"s",
"t",
"u",
"u",
"u",
"u",
"y",
"r",
"z",
"A",
"A",
"C",
"D",
"E",
"E",
"I",
"L",
"L",
"N",
"O",
"O",
"O",
"O",
"R",
"S",
"T",
"U",
"U",
"U",
"U",
"Y",
"R",
"Z"
);
public static function createRandomWord($length = 10) {
for ($word = ""; strlen($word) < $length;) {
$i = mt_rand(0, sizeof(self :: $letters) - 1);
$word .= self :: $letters[$i];
}
if (in_array($word, self :: $randomWordsCache))
$word = self :: createRandomWord($length);
self :: $randomWordsCache[] = $word;
return $word;
}
/**
* Static method, which provides escape string function with additional
* properties (actual character encoding safe escaping)
*
* If $input parameter is an array, it will escape whole array. If it is a
* string, it will return a classic escaped string.
*
* @param string|array $input
* @return string|array
*/
public static function Escape($input) {
if (!get_magic_quotes_gpc()) {
if (is_array($input)) {
for ($i = 0; $i < sizeof($input); $i++) {
$input[$i] = addslashes($input[$i]);
}
} else {
$input = addslashes($input);
}
}
return $input;
}
public static function Unescape($input) {
if (!get_magic_quotes_gpc()) {
if (is_array($input)) {
for ($i = 0; $i < sizeof($input); $i++) {
$input[$i] = stripslashes($input[$i]);
}
} else {
$input = stripslashes($input);
}
}
return $input;
}
/**
* Converts given email address to address/format specified by XCS
* configuration.
*
* @param string $email
* @return string
*/
public static function parseEmail($email) {
$retval = $email;
$address = explode("@", $email);
if (sizeof($address) == 2)
$retval = $address[0] . "[ at ]" . $address[1];
return $retval;
}
/**
* Removes diacritics(national characters) from given string. Useful for
* search optimalization and TEXT2URL converting.
*
* @param string $text
* @return string
*/
public static function removeDiacritics($text) {
$diacritic = array (
"á",
"ä",
"Ä",
"Ä",
"é",
"Ä",
"Ã",
"ĺ",
"ľ",
"Å",
"ó",
"ô",
"Å",
"ö",
"Å",
"Å¡",
"Å¥",
"ú",
"ů",
"ű",
"ü",
"ý",
"Å",
"ž",
"Ã",
"Ã",
"Ä",
"Ä",
"Ã",
"Ä",
"Ã",
"Ĺ",
"Ľ",
"Å",
"Ã",
"Ã",
"Å",
"Ã",
"Å",
"Å ",
"Ť",
"Ã",
"Å®",
"Ű",
"Ã",
"Ã",
"Å",
"Ž"
);
$removed = array (
"a",
"a",
"c",
"d",
"e",
"e",
"i",
"l",
"l",
"n",
"o",
"o",
"o",
"o",
"r",
"s",
"t",
"u",
"u",
"u",
"u",
"y",
"r",
"z",
"A",
"A",
"C",
"D",
"E",
"E",
"I",
"L",
"L",
"N",
"O",
"O",
"O",
"O",
"R",
"S",
"T",
"U",
"U",
"U",
"U",
"Y",
"R",
"Z"
);
return str_replace($diacritic, $removed, $text);
}
public static function encodeFilename($filename) {
$text = trim($filename);
$text = preg_replace('/\&(.*?);/', '', $text);
$text = strip_tags($text);
$text = self :: removeDiacritics($text);
$text = strtolower($text);
$match = array ();
preg_match_all('/[a-zA-Z0-9\.]+/', $text, $match);
$text = implode("-", $match[0]);
return $text;
}
public static function removeSpecialChars($string, $spacer = ' ') {
$string = strip_tags(trim($string));
$string = strtolower(self :: removeDiacritics($string));
$searched = array (
"_",
":",
",",
"-",
"(",
"%",
"&",
"@",
")",
";",
"!",
"$",
">",
"<",
"?"
);
return str_replace($searched, $spacer, $string);
}
public static function formatFilesize($bytes) {
$kilobytes = $bytes / 1024;
if ($kilobytes > 1024)
$megabytes = $kilobytes / 1024;
else
return ceil($kilobytes) . " kB";
if ($megabytes > 1024)
$gigabytes = $megabytes / 1024;
else
return ceil($megabytes) . " MB";
if ($gigabytes > 1024)
$terrabytes = $gigabytes / 1024;
else
return ceil($gigabytes) . " GB";
return ceil($terrabytes) . " TB";
}
public static function getFileType($filename) {
$filename = explode(".", $filename);
return $filename[sizeof($filename) - 1];
}
/**
* Converts a string to right format so it can be used as URL name. Useful
* for rewritting URLs. For example: it replaces all spaces to '-'
* character.
*
* @param string $text My New Article!
* @return string my-new-article
*/
public static function TEXT2URL($text) {
$text = trim($text);
$text = preg_replace('/\&(.*?);/', '', $text);
$text = strip_tags($text);
$text = self :: removeDiacritics($text);
$text = strtolower($text);
$match = array ();
preg_match_all('/[a-zA-Z0-9]+/', $text, $match);
$text = implode("-", $match[0]);
return $text;
}
public static function generateKeywords($keywords) {
$text = trim($keywords);
$text = preg_replace('/\&(.*?);/', '', $text);
$text = strip_tags($text);
$text = self :: removeDiacritics($text);
$text = strtolower($text);
$match = array ();
preg_match_all('/[a-zA-Z0-9]+/', $text, $match);
$text = implode(" ", $match[0]);
return $text;
}
public static function TITLE2FILENAME($text) {
$text = trim($text);
$text = preg_replace('/\&(.*?);/', '', $text);
$text = strip_tags($text);
$text = self :: removeDiacritics($text);
$text = strtolower($text);
$match = array ();
preg_match_all('/[a-zA-Z0-9]+/', $text, $match);
$text = implode("_", $match[0]);
return $text;
}
/**
* This method returns a substring of a given string.
*
* It will break the string in desired chars.For example: you can break your
* string in all white spaces and get the desired string, which will have
* not cutted words - useful for cutting articles/headings.
*
* @param string $text Original string
* @param int $lim Desired string length
* @param string $break A character that will be used for breaking given strings.
* @param string $tail A string that will be placed at the end of cutted string - eg. "..." or hypertext link,etc.
* @return string Returns new value.
*/
public static function Shorten($text, $lim, $break, $tail) {
$text = strip_tags($text);
$text = split("$break", $text);
if (strlen(implode("$break", $text)) >= $lim) {
$i = 0;
$add_str = "";
while ($i <= count($text)) {
$add_str = $text[$i];
$out[] = $add_str;
if (strlen(implode("$break", $out)) >= $lim -strlen($break))
break;
$add_str = "";
$i++;
}
$text = implode("$break", $out);
if (substr($text, 0, -strlen($break)) == $break)
$text = substr($text, 0, -strlen($break));
$text = "$text$tail";
} else {
$text = implode("$break", $text);
}
return $text;
}
public static function CountWords($html) {
$string = strip_tags($html);
$searched = array (
".",
",",
":",
"-",
"_"
);
$removed = array (
" ",
" ",
" ",
" ",
" "
);
$string = str_replace($searched, $removed, $string);
$words = explode(" ", $string);
$word_num = 0;
foreach ($words as $word) {
//if (strlen($word) > 2)
$word_num++;
}
return $word_num;
}
public static function addProtocol($string, $protocol = 'http://') {
if (strpos($string, $protocol) === false) {
return $protocol . $string;
} else
return $string;
}
public static function addURLDelimiter($url) {
if (strpos($url, "?") === false) {
$url .= "?";
} else {
$url .= "&";
}
return $url;
}
public static function clearLeadingZeros($number) {
$pos = strpos((string) $number, '0');
if ($pos === true) {
$number = substr($number, 1);
}
return (int) $number;
}
public static function cleanXHTML($invalidHTML) {
if (!extension_loaded('tidy'))
return $invalidHTML;
$tidy = new tidy;
$config = array (
'clean' => true,
'drop-proprietary-attributes' => true,
'show-body-only' => true,
'output-xhtml' => true,
'indent' => true,
'quiet' => true,
'write-back' => true,
'wrap' => 0
);
$tidy->parseString($invalidHTML, $config, 'UTF8');
$tidy->CleanRepair();
return $tidy;
}
public static function cleanXML($invalidXHTML) {
if (!extension_loaded('tidy'))
return $invalidXHTML;
$tidy = new tidy;
$config = array (
'clean' => false,
'show-body-only' => true,
'output-xhtml' => true,
'quiet' => true,
'numeric-entities' => true
);
$tidy->parseString($invalidXHTML, $config, 'UTF8');
$tidy->CleanRepair();
return $tidy;
}
public static function cleanXHTMLDocument($invalidHTML) {
if (!extension_loaded('tidy'))
return $invalidHTML;
$tidy = new tidy;
$config = array (
'clean' => false,
'drop-proprietary-attributes' => false,
'output-xhtml' => true,
'show-body-only' => false,
'indent' => true,
'quiet' => true,
'write-back' => true,
'wrap' => 0
);
$tidy->parseString($invalidHTML, $config, 'UTF8');
$tidy->CleanRepair();
return $tidy;
}
public static function Serialize($string) {
if (isset ($string))
return serialize($string);
else
return NULL;
}
public function isRude($text) {
$rude_words = i18n :: TranslateDEPRECATED("ban", "rude_words");
$rude_words = explode(",", $rude_words);
foreach ($rude_words as $rude) {
if (strpos($text, $rude)) {
return true;
}
}
return false;
}
}
?>
See more files for this project here