src/StringHelpers.php
<?php
namespace pwf\helpers;
class StringHelpers
{
/**
* Hast string
*
* @param string $string
* @param int $iteration
* @return string
*/
public static function hashString($string, $iteration = 5)
{
$result = $string;
for ($i = 0; $i < $iteration; $i++) {
$result = md5($result);
}
return $result;
}
/**
* Translit ru->en
*
* @param string $string
* @return string
*/
public static function translit($string)
{
$string = strtr($string,
"абвгдежзийклмнопрстуфыэАБВГДЕЖЗИЙКЛМНОПРСТУФЫЭ",
"abvgdegziyklmnoprstufieABVGDEGZIYKLMNOPRSTUFIE"
);
$string = strtr($string,
array(
'ё' => "yo", 'х' => "h", 'ц' => "ts", 'ч' => "ch", 'ш' => "sh",
'щ' => "shch", 'ъ' => '', 'ь' => '', 'ю' => "yu", 'я' => "ya",
'Ё' => "Yo", 'Х' => "H", 'Ц' => "Ts", 'Ч' => "Ch", 'Ш' => "Sh",
'Щ' => "Shch", 'Ъ' => '', 'Ь' => '', 'Ю' => "Yu", 'Я' => "Ya",
));
return $string;
}
/**
* Translit
*
* @param string $string
* @return string
*/
public static function rus2translit($string)
{
$converter = array(
'а' => 'a', 'б' => 'b', 'в' => 'v',
'г' => 'g', 'д' => 'd', 'е' => 'e',
'ё' => 'e', 'ж' => 'zh', 'з' => 'z',
'и' => 'i', 'й' => 'y', 'к' => 'k',
'л' => 'l', 'м' => 'm', 'н' => 'n',
'о' => 'o', 'п' => 'p', 'р' => 'r',
'с' => 's', 'т' => 't', 'у' => 'u',
'ф' => 'f', 'х' => 'h', 'ц' => 'c',
'ч' => 'ch', 'ш' => 'sh', 'щ' => 'sch',
'ь' => '\'', 'ы' => 'y', 'ъ' => '\'',
'э' => 'e', 'ю' => 'yu', 'я' => 'ya',
'А' => 'A', 'Б' => 'B', 'В' => 'V',
'Г' => 'G', 'Д' => 'D', 'Е' => 'E',
'Ё' => 'E', 'Ж' => 'Zh', 'З' => 'Z',
'И' => 'I', 'Й' => 'Y', 'К' => 'K',
'Л' => 'L', 'М' => 'M', 'Н' => 'N',
'О' => 'O', 'П' => 'P', 'Р' => 'R',
'С' => 'S', 'Т' => 'T', 'У' => 'U',
'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C',
'Ч' => 'Ch', 'Ш' => 'Sh', 'Щ' => 'Sch',
'Ь' => '\'', 'Ы' => 'Y', 'Ъ' => '\'',
'Э' => 'E', 'Ю' => 'Yu', 'Я' => 'Ya',
);
return strtr($string, $converter);
}
/**
* String to url
*
* @param string $str
* @return string
*/
public static function str2url($str)
{
$str = self::rus2translit($str);
$str = strtolower($str);
$str = preg_replace('~[^-a-z0-9_]+~u', '-', $str);
$str = trim($str, "-");
return $str;
}
/**
* Minify string
*
* @param string $str
* @return string
*/
public static function minify($str)
{
$matchesCode = $matchesPre = $matches = [];
preg_match_all('/<code.*?>(.*?)<\/code>/mis', $str, $matchesCode);
preg_match_all('/<pre.*?>(.*?)<\/pre>/mis', $str, $matchesPre);
$matches = array_merge($matchesCode[1], $matchesPre[1]);
foreach ($matches as $match) {
$str = str_replace($match, '$'.md5($match), $str);
}
$str = preg_replace(['/<!--(.*)-->/Uis', "/[[:blank:]]+/"], ['', ' '],
str_replace(["\n", "\r", "\t"], '', $str));
foreach ($matches as $match) {
$str = str_replace('$'.md5($match), $match, $str);
}
return $str;
}
}