Skip to content

Instantly share code, notes, and snippets.

@namreg
Last active August 29, 2015 14:03
Show Gist options
  • Save namreg/6d457719ea128a600bc1 to your computer and use it in GitHub Desktop.
Save namreg/6d457719ea128a600bc1 to your computer and use it in GitHub Desktop.
Test task solution for Metagile
<?php
/**
* @author namreg <iggerman@yandex.com>
*/
$input1 = 'bcdef abd uiop fg';
$input2 = '012 23 24 ц ц';
$input3 = 'βγ γβ γ АБВ';
echo sprintf('%s -> %s', $input1, process_string($input1)) . PHP_EOL;
echo sprintf('%s -> %s', $input2, process_string($input2)) . PHP_EOL;
echo sprintf('%s -> %s', $input3, process_string($input3)) . PHP_EOL;
function process_string($string)
{
$words = array();
foreach (mb_split('\s', $string) as $word) {
// удалям лишние пробелы. могут возникнуть, кодга несколько пробелов между словами
$word = preg_replace('/\s/', '', $word);
$wordLength = mb_strlen($word, 'UTF-8');
// если слово состоит только из одной буквы - оно нам подходит
if ($wordLength == 1) {
$words[] = $word;
continue;
}
$isWordValid = true;
$previousOrd = ord_utf8(mb_substr($word, 0, 1));
foreach (mb_string_to_array($word) as $k => $char) {
$ord = ord_utf8($char);
$diff = $ord - $previousOrd;
if ($diff == 1 || $k == 0) {
$previousOrd = $ord;
} else {
$isWordValid = false;
break;
}
}
if ($isWordValid) {
$words[] = $word;
}
}
return implode(' ', $words);
}
// convert utf8 string to array of chars
function mb_string_to_array($string)
{
$length = mb_strlen($string, 'UTF-8');
$array = array();
while ($length) {
$array[] = mb_substr($string, 0, 1, 'UTF-8');
$string = mb_substr($string, 1, $length, 'UTF-8');
$length = mb_strlen($string, 'UTF-8');
}
return $array;
}
// implementation of ord() function for utf8 charachter
//http://stackoverflow.com/a/9361531
function ord_utf8($c)
{
if (ord($c{0}) >=0 && ord($c{0}) <= 127)
return ord($c{0});
if (ord($c{0}) >= 192 && ord($c{0}) <= 223)
return (ord($c{0})-192)*64 + (@ord($c{1})-128);
if (ord($c{0}) >= 224 && ord($c{0}) <= 239)
return (ord($c{0})-224)*4096 + (ord($c{1})-128)*64 + (ord($c{2})-128);
if (ord($c{0}) >= 240 && ord($c{0}) <= 247)
return (ord($c{0})-240)*262144 + (ord($c{1})-128)*4096 + (ord($c{2})-128)*64 + (ord($c{3})-128);
if (ord($c{0}) >= 248 && ord($c{0}) <= 251)
return (ord($c{0})-248)*16777216 + (ord($c{1})-128)*262144 + (ord($c{2})-128)*4096 + (ord($c{3})-128)*64 + (ord($c{4})-128);
if (ord($c{0}) >= 252 && ord($c{0}) <= 253)
return (ord($c{0})-252)*1073741824 + (ord($c{1})-128)*16777216 + (ord($c{2})-128)*262144 + (ord($c{3})-128)*4096 + (ord($c{4})-128)*64 + (ord($c{5})-128);
if (ord($c{0}) >= 254 && ord($c{0}) <= 255) // error
return FALSE;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment