你好,我有麻烦的php代码拼字游戏点计数器,但在拉脱维亚

0wi1tuuw  于 2023-02-03  发布在  PHP
关注(0)|答案(1)|浏览(87)

所以海峡追逐

<?php declare(strict_types=1); // strict ment

function punktudaudzums($mot){
    
    $b10punkti = array("f","ģ","h","ķ","č");
    $b8punkti = array("ļ","ž");
    $b6punkti = array("ņ","š","ū");
    $b5punkti = array("b","c","g");
    $b4punkti = array("ē","ī","j");
    $b3punkti = array("d","o","v","z");
    $b2punkti = array("ā","k","l","m","n","p");
    $b1punkti = array("a","e","i","r","s","t","u");
    
    //$mot = utf8_encode($mot);

    $total = 0;
    for ($i=0; $i < strlen($mot) ; $i++) {
        //echo strlen($mot);
        $char = $mot[$i];
        //echo $char;
        if (in_array($char, $b10punkti)) {
            $total += 10;
        }
        elseif (in_array($char, $b8punkti)) {
            $total += 8;
        }
        elseif (in_array($char, $b6punkti)) {
            $total += 6;
        }
        elseif (in_array($char, $b5punkti)) {
            $total += 5;
        }   
        elseif (in_array($char, $b4punkti)) {
            $total += 4;
        }   
        elseif (in_array($char, $b3punkti)) {
            $total += 3;    
        }
        elseif (in_array($char, $b2punkti)) {
            $total += 2;    
        }
        else{
            $total += 1;
        }
    }
    return $total;
}

//echo punktudaudzums("f\xC4\xA3h\xC4\xB7\xC4\x8D");

echo punktudaudzums("fģhķč");

//('f\xC4\xA3h\xC4\xB7\xC4\x8D')

//$b10punkti = array("f","ģ","h","ķ","č");

//print_r($b10punkti);

它是一个程序,给字符赋值,当你给随机字符串时,它计算总和,所以当你给fhk时,它需要50,但它不是,它输出26和utf8 32。
我尝试在utf8中编码没有帮助,然后mb_strlen它修复了在数组中测试的输出$mot

<?php
$test = array("č");
if (in_array("č", $test)) {
    echo "Got č";
}
else "does not have č";
``` im expecting 50 value
mklgxw1f

mklgxw1f1#

还有另一个解决方案for ($i=0; $i < mb_strlen($mot); $i++) { $char = mb_substr($mot, $i, 1);,但劳伦斯切龙foreach (mb_str_split($mot, 1) as $char) {更好,再次感谢

相关问题