/**
 * @param string|null $arg
 * @return string
 * @throws Exception
 */
function toEmacs($arg = null)
{
    if (!$arg) {
        throw new \Exception('PAS DE ARG');
    }
    $fileContent = file_get_contents($arg);
    $result = [];
    $blanks = [];
    $blankGroupIndex = 0;
    $newLine = true;
    $onBlank = false;
    $pos = 0;
    foreach (str_split($fileContent) as $char) {
        if ($newLine) {
            $blanks = [];
            $blankGroupIndex = 0;
            $newLine = false;
            $pos = 0;
            $onBlank = false;
        }
        if ($char === ' ' || chr(9) === $char) {
            if (!$onBlank) {
                $blanks[$blankGroupIndex] = 0;
            }
            $actualBlankLen = getLen($pos, $char);
            $blanks[$blankGroupIndex] += $actualBlankLen;
            $pos += $actualBlankLen;
            $onBlank = true;
            continue;
        } elseif ($onBlank) {
            $onBlank = false;
            $result[] = [' ', $blanks[$blankGroupIndex++]];
        }
        if ($char === PHP_EOL) {
            $newLine = true;
        }
        $result[] = [$char, 1];
        $pos++;
    }
    $emacsed = '';
    foreach ($result as $charData) {
        $emacsed .= getRepresentation(0, $charData);
    }
    return $emacsed;
}
示例#2
0
<?php

function getLen(string $str)
{
    $len = strlen($str);
    if (!$len) {
        return 0;
    }
    $count = 0;
    for ($i = 0; $i < $len; $i++) {
        if (ord($str[$i]) >= 0x80) {
            $i += 2;
            //utf-8 3个字节一个中文
            //++$i;			//gbk 2个字节一个中文
        }
        ++$count;
    }
    return $count;
}
$str = '中国so好!';
echo getLen($str);