【问题标题】:Notice: Uninitialized string offset: 1注意:未初始化的字符串偏移量:1
【发布时间】:2017-05-04 23:18:08
【问题描述】:

我在第 66 行的这段代码中有问题 注意:未初始化的字符串偏移量:第 66 行为 1

<?php
echo cologne_phon("d");
echo "<br />";
echo cologne_phon("ch dc");
echo "<br />";
echo cologne_phon("dc"); 

function cologne_phon($word){

    /**
     * @param  string  $word string to be analyzed
     * @return string  $value represents the K?lner Phonetik value
     * @access public
     */

    //prepare for processing
    $word=strtolower($word);
    $substitution=array(
                        "?"=>"a",
                        "?"=>"o",
                        "ü"=>"u",
                        "?"=>"ss",
                        "ph"=>"f"
                        );

    foreach ($substitution as $letter=>$substitution) {
        $word=str_replace($letter,$substitution,$word);
    }

    $len=strlen($word);

    //Rule for exeptions
    $exceptionsLeading=array(
                             4=>array("ca","ch","ck","cl","co","cq","cu","cx"),
                             8=>array("dc","ds","dz","tc","ts","tz")
                             );

    $exceptionsFollowing=array("sc","zc","cx","kx","qx");

    //Table for coding
    $codingTable=array(
                       0=>array("a","e","i","j","o","u","y"),
                       1=>array("b","p"),
                       2=>array("d","t"),
                       3=>array("f","v","w"),
                       4=>array("c","g","k","q"),
                       48=>array("x"),
                       5=>array("l"),
                       6=>array("m","n"),
                       7=>array("r"),
                       8=>array("c","s","z"),
                       );

    for ($i=0;$i<$len;$i++){
        $value[$i]='';

        //Exceptions
        if ($i==0 && $word[$i]=='cr') 
            $value[$i]=4;

        foreach ($exceptionsLeading as $code =>$letters) {
            if (in_array($word[$i].$word[$i+1],$letters)){
                $value[$i]=$code;
            }
        }

        if ($i!=0 && (in_array($word[$i-1].$word[$i], 
                               $exceptionsFollowing))) {
            $value[$i]=8;        
        }                

        //Normal encoding
        if ($value[$i]==""){
            foreach ($codingTable as $code=>$letters) {
                if (in_array($word[$i],$letters))$value[$i]=$code;
            }
        }
    }

    //delete double values
    $len=count($value);

    for ($i=1;$i<$len;$i++){
        if ($value[$i]==$value[$i-1]) $value[$i]="";
    }

    //delete vocals 
    for ($i=1;$i>$len;$i++){//omitting first characer code and h
        if ($value[$i]==0) $value[$i]="";
    }

    var_dump($value);
    $value=array_filter($value);
    $value=implode('',$value);

    return $value;
}  

我尝试了很多修复方法,但对我没有任何作用,所以需要帮助 php站点中的原始代码 http://php.net/manual/en/function.soundex.php#84881

【问题讨论】:

标签: php


【解决方案1】:

问题出在这一行:

        if (in_array($word[$i].$word[$i+1],$letters)){

在循环的最后一次迭代中,$word[$i]$word 的最后一个字符,并且没有$word[$i+1]。您应该更改它以检查您是否不在字符串的末尾:

        if ($i < $len - 1 && in_array($word[$i].$word[$i+1],$letters)){

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-21
    • 2012-09-21
    • 2019-08-11
    • 2017-05-10
    相关资源
    最近更新 更多