Transforming a Javascript function for PHP

Asked

Viewed 176 times

1

I’m trying to pass a function written in javasript to php,but I think I was wrong in translating some command and I can’t find the error. Javascript function:

<script>

    function dec(string)
        {// retorna a matris pura, descompactada
            var myArray = [];
            var str, str2, arr, arr2, lat, lng, f;
            var strings = string.split(" ");
            for(var i=0; i<strings.length; i++)
            {
                str = strings[i];
                str2 = '';
                arr = [];
                arr2 = [];
                for (var j=0; j<str.length; j++)
                {
                    switch (str.charAt(j))
                    {
                        case 'A': str2 += ',0'; break;
                        case 'B': str2 += ',1'; break;
                        case 'C': str2 += ',-1'; break;
                        case 'D': str2 += ',2'; break;
                        case 'E': str2 += ',-2'; break;
                        case 'F': str2 += ',3'; break;
                        case 'G': str2 += ',-3'; break;
                        case 'H': str2 += ',4'; break;
                        case 'I': str2 += ',-4'; break;
                        case 'J': str2 += ',5'; break;
                        case 'K': str2 += ',-5'; break;
                        case 'L': str2 += ',6'; break;
                        case 'M': str2 += ',-6'; break;
                        case 'N': str2 += ',7'; break;
                        case 'O': str2 += ',-7'; break;
                        case 'P': str2 += ',8'; break;
                        case 'Q': str2 += ',-8'; break;
                        case 'R': str2 += ',9'; break;
                        case 'S': str2 += ',-9'; break;
                        default: str2 += str.charAt(j); break;
                    }
                }
                arr = str2.split(",");
                f = arr.shift();
                lng = parseInt(arr[0])/f;
                lat = parseInt(arr[1])/f;
                arr2.push([lat, lng]);
                for (var j=2; j<arr.length; j+=2)

                {
                    lng += parseInt(arr[j])/f;
                    lat += parseInt(arr[j+1])/f;
                    arr2.push([lat, lng]);
                }
                myArray.push(arr2);
            }
            return myArray;
        }
    dec("1000I6575E3374BCBAACBABABABABBABBAABBBABBAABBABBDABAABBBABBAABBAABBABAABBBABBAABBBABBBBAABBAACBABCBABAABBABCBAACBCACBABAACBABAACBAAAACCAACCCACBCACACCACCCCCAACCCCCCACAACACACACACACACACACACCCACAACAACCCCCCCCCCACCACCCACCACCACACACBAACACACCAACACCAACACBAACACACCACCCACAACCACAACAACAACCACAACCACAACACBAACAAACACACCAACBCAAACACACACACACCAACACCAACACBAACACBAACBAACACACACACACCACAACACACACACCAACACCAACBAACACCAACCAACACACCCACACBAACBAACACACBAACACBAACBAACACBCBCBCACBAACACACBAACACACACACACACACACACACACACBCACACACACACCEACCEACACBAACACBAACBAACACAAACACBABCBAACBAABBBBABBBABABABABAABBABBBAABBBBBABBAABBBBBBBBABBBAABBBABBABAABBABABAABBAABBABBABBBBAABBBBAABBABABBABBABABBBABBBABAABBABABABBBABABABAABBABABABABABABABBBABABABCBABABAACBABBBABADABABABABABCBABABABCBABCBABCBABAACBABABAACBABABABABABABBACBABABBBABABABABAABBABABABABBBABABAABBAACBABABABAABBABAABCBABBABABAACBCBAABCAABBABAABABABBABAABBABABCBABABABAACBABAAAABBABBACBAACBABAABCAABBAABBABAABBABAACACBABAACCCCAACBABAACACBCBAABABBAACACBACCCABCBAACCACAACACCAACACBAACBABAABBAACBAACBAABABBAABABBABCBABAABBAABBAABBABABABAACBABABAABABBAABCAABABABBAABAAAABAACBAABAAABABBAABBAACACBAACACBAACACBAACBAACCAACBAACACACBAACBAACACBAACBCACBAACBAACACBAACBAACACACBAACACBAACBAACACBAACBAACACACACCAACACACACACCACACACBCACACAACCACCACBAACCAACCACACACACAACCACAACCAACACACCACACCAAACBABAACCAACCAACCACAACCAACCAACCAACCAACCBCACAACAAACBAACACACACACACBAACBCACBABAACBCACBAACACACCCCAECCACACAAEBCBAACACBACCACBAACAAACACBAACBABCBABAACBABABCBABABABAAAACACECCCACAAACCABCCCBAACBABAACACBABABAACACCACCCACACCCAACACBABCBAACACBAACACBCACBAACACBAACCAABCAABCAACCAACCAACCCACACACCAACACACBAACACCAACCACACACACACCCCCAABCACACACAACCCCCCAACAAACACBABCBAACBCACACACCAACCCCAACACACCAACCAACCAACCACCCACACACAACACCAACACBCACACACACCAACCACACCCCCACAACACACCAACACCACAACCACACACAACACACCACACAABABABABCACBABCAABCACACBCCCAACACACBAACCAACCACACACBCCCACACACACCCCCACACACACBCACACAABCACAABCACACACAACCACAABCACAABCAABCACACAABCAACCBACCACACACACACAABABCAABABCAABABCAABAAABAAABCAABABABABCAABCCABCACBCACACACAABCACAACCACACAACCAABCACCCACAABCAACCACAABCACACAACCACACCCACACAABCACBCAABCAABCAABABCAABCAABCACACACBABCACBCAABCACACACACACACACACACAACCBCACACACAABCACBCAABCACAABCACBCAABCAABCAABCACACACACAACCAACCAACCCACCACCCACACBCACBCBCACBCAABCACBCAABCAABCACAABCACBCBCACACAABCACAACACACACACACCAACACACAECCACAEAECEACACACCCAEACACBAACBAACACCCACACCACAACCAACACBCACAEACBACCACACCAACACCACCCAACCAACCACAACCACAACCACAACCAACCACACACAACCACAACCAACCACACACAACCACCCAACCACACAACCACAABCAABCAABCCEAACCCCAACCACBCAABCAACACCCACACACACACACACCAACACCACAACCAACACACACACBAACACCCCAACCAACCAACBAACACACCCACCCCCACBABABAACACBAACACACACBABABCACACBAACBAACBCBBBAACACACACBAABBCACBABABABAACBAACACBAACCAACCAACCAACCACAACCACACCCACACACCCCCCCCCCCCCCCCCAACCAACCACAACCAECCAECCAEACCCACCCABKBAAEBEACDSAEAEBEBCBEDELOBEFGDGJKACBCBCACACAGAC2ACACACACAKACACACACCCAEAECAACCCCCCAACCAACEECCCCCCACACACACACCEAGAGBEBOBEBCDCDADEDCDCDEDCACAEACACACACACACBCACACACACACACCAACACACACAECAACACACACACBCACBCCCBEBCCCBCACACACBAACACACACBAACHBEECCCAACACBAACACACACDAACBAACACCAACACBCACACACACCACAACBCBCACCACACAACACCAACCAAAEACAACACACBCACACBABABCDABBDAACBAACCCACCAACBCBECCBCCAACACCACAACCAACDEBAACBAACACACACCAACAAACCACAACBABACCBCACCCCACAACAACCACACACCAABABCACBCAACCACACCACCAACCACACACACAACCACACAACCCABCAACAACAACCAACCACAACCACAACBAACACACBCACBAACACACACACBAACBAACBABAACCAACCACACACCACCCCACBCAACCAACACCAACCACACAACCACACACCCACCCAAACAABCBABCBABCACAABCBABCACBCACAABABCBCBCACACACACACACCCACACACACACACACACBCACAABCAABCBABABCAABCACACCACCACCCACBCACACCACCAACCACCCAACCAACCACACCCACCCAABCACACACACCACCACCCACAACCAACCACACACACBCAABABCACACCCACACACACCCACACACACACAABCACBCAABCACACACACACACAACACCACACACACACAACBAACCACAAACACACACCCAACACCAACCCCCACCACACCACACCACACACAABCAACCAACCCCACACAABABCACAABABCAABBAABBABBBAABBABAABBAABABABBBABBBCBBAABBBABBAABBABAABABBAABBAABABABBAABBAABCAABABBABAABBAABBAABABABABCACAACCAABCAABCBCBCAABCAABCAABBAABABABCBCACBCACCCAABCACACAABCBCBABCAABABBAABCBABBBABCACACACACBCACAABCAABCACAABBAABCAABCAABCBCACBCACAACCAACCACACACBCBABCAABCACAABCACACACACACCCACBABABABABCBABABABABABCBABCBABABABABCBABABBACBCAABBAABABCAABABBAABCAABBACBBAABAAABABBAABBAABABBAABBAABABBBABBAABABABBAABCAABBACBABCAAACAABABABABAABABCBBACBABAACBAACACACACBAACBABAABBABABCBABADABABCBABAABBABADADABABBBABAABBJADABAFANAPAB0ABCAABCAABCBABCABBABCAABBAABABABABCAABABBABABABAACBABCBABBACBABBBABAACBAABBABABAABABBAABABABABABABABCACCABCAABBABAABBAABABABABABABCACBCAABCAABCAABCAABCAABCACAABCAABCACAABCACAABCACBCAABCACAABCACACACACAABCACAABCACACACACACACACACAABCACACAABCAABABABBAABAAABBAABCAABBAAABAABBAABBABAABABABABBAABBAABBBBAABABBABAABBAABCAAAABAABAAABBABCACAABBACBABAAABCBABABABBBABABBABBDBDABBBABABBABBBBBABBABBBABABAABBBBAABBDABBDABABABABCBCACBCACCCACAABCAABCAABCACAABCCACCAABCAABCAABCACAABCACBCBCAABCBBBABCACAABCACACBCAABABABABCBABCAABABCBABBAABDBBAABABBAABBBABBBCAABBAABABABCAABBAABABCAABABABABABAAABBAABABABBAABAAABCAABAAABCAABCACACACBCACACBCAABBABABAABBAABABCAABCBABABCBABCAABCAABBACBBAABABBAABABABABABBBABABBAABBAABABBAABABBAABABABABABABBAABABAAABABABABBAABABABABBAABABABABAAABCAABABBABBBBBAABABABBAABBBBABAABABBABABABAABBAABBABAABAAABBAABABABBAABBABAABBABAABBABAABBAABBAABABBAABBAABBAABBAABBBBAABBAABABABBAABBAABBABAABBAABBAACBAABBABAABABABABBBBAABBBCACAABABCACAABCACACBCACAABCAABABCBABCAACCAACCAACCAACCACACACCCACACACACACAACCCACCACAEBCCACCACACACACACAACCACAACCACACBCACCCACACAABCACACAACACCCCACACACCCACACAAAABCBABABAAABABCAABBAABCAABABABBAABABBBCAABBAABABBBABBAABABCAABCAABABABBABABABABBAABABABABAACBBBABAABABBABAABBAABABBABBBCBBBAABABBAABBABABCBAABBBBBABBABABABABBBABAABBAABABABABBABABDBAAABAACBABAABABABBAABABBABBABABBAABBAABABBAABABCAABABBAABCAABCBABABABBAABABABABABABABCAABBAABABABABABABDBBDBBBABABABABABABAABBAABABBDCACAACCACACAABCAABABABCACAABEACAABABAAABCACAABABCBAAABCACBABCAABABBAABBAABABBAABBAABBABABAABABABBAABAAABCAABCACAABCACACACACAABCACAABCAABABABCAABABCAABCBABABABABADABABABCAABCACAABCAABCAABABCBABABBAABABABBAABBABAABABBBABBABAABCACAACCACAACCAACACCCCCACCACACACACAABCAACCAACCACACACAABCAABCAABCACAABCAAACACAACCACAACCCACBAACCAACACACCACAACCACBBBABCAABCBCBCBCBABCAABABABABABABABCAABCAABABCAABABABABABABABCAABBBABCAABCACAABABABABABCAABCAABCAABCAABABABCACACBCAABCAABCAABCBABABABABBAABBABAABABABABABCAABCAABABCACAABCAABCBABBAABBAABCAABCBBAABBAABBAABBAACBAABBAABBABAAABAABBCBABABAABBABABCBABAABBAABAABAABBABABAABCAABBABAACBAACBAACBAACACCAACCAACBAACBAABBAACBABACBABBAABBABABABAACBAACAAAABABAACAAACBABAACBABBBABAABABABCAABABBABAABBABABCBABABACBBBBABABABABAAAABABAAABBAABCAABBAABABABABBAACBAABABBABAABBAABBAACBABCBAABBAACBAABBABCBABABAACACBAACBABABAABBAAABAACBAABBCABBABAAABABAACBBBABABABABBBAACBAACACBAACBAACBAACBABABAACBABABABABAACBAACBABCACACACACACACBAACBCBCBABABABABABBACBAACACBCACACACACACACBAAABAACBABABABABCABBABAACACACACACACACBABABABABABABBBABCBCBCBCBABAABBBABABABABABABBABBBAABBABABAABBABBACBADEBCBABABABABAACBAACBCBCACBABCBABABCBCACBCACBAACBABABBBABABBBABABABABBBABABABABADBBABCBABAACBABABBBAABABBABABDBBBABADABAABBAABABCBCBABBABABAACBABADABABABAABBACBCBABABABABBABAABBACDABABBBBBABBBABBAABBAABBBBAABBABABBBABBBAABABABCBCAABABABBABBBABAABBABBBABABABBBBBBABBABABAABBAABBAABBAABBABAABABBBBABABABABA");


    </script>

Function in PHP:

Descompacta('1000I6575E3374BCBAACBABABABABBABBAABBBABBAABBABBDABAABBBABBAABBAABBABAABBBABBAABBBABBBBAABBAACBABCBABAABBABCBAACBCACBABAACBABAACBAAAACCAACCCACBCACACCACCCCCAACCCCCCACAACACACACACACACACACACCCACAACAACCCCCCCCCCACCACCCACCACCACACACBAACACACCAACACCAACACBAACACACCACCCACAACCACAACAACAACCACAACCACAACACBAACAAACACACCAACBCAAACACACACACACCAACACCAACACBAACACBAACBAACACACACACACCACAACACACACACCAACACCAACBAACACCAACCAACACACCCACACBAACBAACACACBAACACBAACBAACACBCBCBCACBAACACACBAACACACACACACACACACACACACACBCACACACACACCEACCEACACBAACACBAACBAACACAAACACBABCBAACBAABBBBABBBABABABABAABBABBBAABBBBBABBAABBBBBBBBABBBAABBBABBABAABBABABAABBAABBABBABBBBAABBBBAABBABABBABBABABBBABBBABAABBABABABBBABABABAABBABABABABABABABBBABABABCBABABAACBABBBABADABABABABABCBABABABCBABCBABCBABAACBABABAACBABABABABABABBACBABABBBABABABABAABBABABABABBBABABAABBAACBABABABAABBABAABCBABBABABAACBCBAABCAABBABAABABABBABAABBABABCBABABABAACBABAAAABBABBACBAACBABAABCAABBAABBABAABBABAACACBABAACCCCAACBABAACACBCBAABABBAACACBACCCABCBAACCACAACACCAACACBAACBABAABBAACBAACBAABABBAABABBABCBABAABBAABBAABBABABABAACBABABAABABBAABCAABABABBAABAAAABAACBAABAAABABBAABBAACACBAACACBAACACBAACBAACCAACBAACACACBAACBAACACBAACBCACBAACBAACACBAACBAACACACBAACACBAACBAACACBAACBAACACACACCAACACACACACCACACACBCACACAACCACCACBAACCAACCACACACACAACCACAACCAACACACCACACCAAACBABAACCAACCAACCACAACCAACCAACCAACCAACCBCACAACAAACBAACACACACACACBAACBCACBABAACBCACBAACACACCCCAECCACACAAEBCBAACACBACCACBAACAAACACBAACBABCBABAACBABABCBABABABAAAACACECCCACAAACCABCCCBAACBABAACACBABABAACACCACCCACACCCAACACBABCBAACACBAACACBCACBAACACBAACCAABCAABCAACCAACCAACCCACACACCAACACACBAACACCAACCACACACACACCCCCAABCACACACAACCCCCCAACAAACACBABCBAACBCACACACCAACCCCAACACACCAACCAACCAACCACCCACACACAACACCAACACBCACACACACCAACCACACCCCCACAACACACCAACACCACAACCACACACAACACACCACACAABABABABCACBABCAABCACACBCCCAACACACBAACCAACCACACACBCCCACACACACCCCCACACACACBCACACAABCACAABCACACACAACCACAABCACAABCAABCACACAABCAACCBACCACACACACACAABABCAABABCAABABCAABAAABAAABCAABABABABCAABCCABCACBCACACACAABCACAACCACACAACCAABCACCCACAABCAACCACAABCACACAACCACACCCACACAABCACBCAABCAABCAABABCAABCAABCACACACBABCACBCAABCACACACACACACACACACAACCBCACACACAABCACBCAABCACAABCACBCAABCAABCAABCACACACACAACCAACCAACCCACCACCCACACBCACBCBCACBCAABCACBCAABCAABCACAABCACBCBCACACAABCACAACACACACACACCAACACACAECCACAEAECEACACACCCAEACACBAACBAACACCCACACCACAACCAACACBCACAEACBACCACACCAACACCACCCAACCAACCACAACCACAACCACAACCAACCACACACAACCACAACCAACCACACACAACCACCCAACCACACAACCACAABCAABCAABCCEAACCCCAACCACBCAABCAACACCCACACACACACACACCAACACCACAACCAACACACACACBAACACCCCAACCAACCAACBAACACACCCACCCCCACBABABAACACBAACACACACBABABCACACBAACBAACBCBBBAACACACACBAABBCACBABABABAACBAACACBAACCAACCAACCAACCACAACCACACCCACACACCCCCCCCCCCCCCCCCAACCAACCACAACCAECCAECCAEACCCACCCABKBAAEBEACDSAEAEBEBCBEDELOBEFGDGJKACBCBCACACAGAC2ACACACACAKACACACACCCAEAECAACCCCCCAACCAACEECCCCCCACACACACACCEAGAGBEBOBEBCDCDADEDCDCDEDCACAEACACACACACACBCACACACACACACCAACACACACAECAACACACACACBCACBCCCBEBCCCBCACACACBAACACACACBAACHBEECCCAACACBAACACACACDAACBAACACCAACACBCACACACACCACAACBCBCACCACACAACACCAACCAAAEACAACACACBCACACBABABCDABBDAACBAACCCACCAACBCBECCBCCAACACCACAACCAACDEBAACBAACACACACCAACAAACCACAACBABACCBCACCCCACAACAACCACACACCAABABCACBCAACCACACCACCAACCACACACACAACCACACAACCCABCAACAACAACCAACCACAACCACAACBAACACACBCACBAACACACACACBAACBAACBABAACCAACCACACACCACCCCACBCAACCAACACCAACCACACAACCACACACCCACCCAAACAABCBABCBABCACAABCBABCACBCACAABABCBCBCACACACACACACCCACACACACACACACACBCACAABCAABCBABABCAABCACACCACCACCCACBCACACCACCAACCACCCAACCAACCACACCCACCCAABCACACACACCACCACCCACAACCAACCACACACACBCAABABCACACCCACACACACCCACACACACACAABCACBCAABCACACACACACACAACACCACACACACACAACBAACCACAAACACACACCCAACACCAACCCCCACCACACCACACCACACACAABCAACCAACCCCACACAABABCACAABABCAABBAABBABBBAABBABAABBAABABABBBABBBCBBAABBBABBAABBABAABABBAABBAABABABBAABBAABCAABABBABAABBAABBAABABABABCACAACCAABCAABCBCBCAABCAABCAABBAABABABCBCACBCACCCAABCACACAABCBCBABCAABABBAABCBABBBABCACACACACBCACAABCAABCACAABBAABCAABCAABCBCACBCACAACCAACCACACACBCBABCAABCACAABCACACACACACCCACBABABABABCBABABABABABCBABCBABABABABCBABABBACBCAABBAABABCAABABBAABCAABBACBBAABAAABABBAABBAABABBAABBAABABBBABBAABABABBAABCAABBACBABCAAACAABABABABAABABCBBACBABAACBAACACACACBAACBABAABBABABCBABADABABCBABAABBABADADABABBBABAABBJADABAFANAPAB0ABCAABCAABCBABCABBABCAABBAABABABABCAABABBABABABAACBABCBABBACBABBBABAACBAABBABABAABABBAABABABABABABABCACCABCAABBABAABBAABABABABABABCACBCAABCAABCAABCAABCAABCACAABCAABCACAABCACAABCACBCAABCACAABCACACACACAABCACAABCACACACACACACACACAABCACACAABCAABABABBAABAAABBAABCAABBAAABAABBAABBABAABABABABBAABBAABBBBAABABBABAABBAABCAAAABAABAAABBABCACAABBACBABAAABCBABABABBBABABBABBDBDABBBABABBABBBBBABBABBBABABAABBBBAABBDABBDABABABABCBCACBCACCCACAABCAABCAABCACAABCCACCAABCAABCAABCACAABCACBCBCAABCBBBABCACAABCACACBCAABABABABCBABCAABABCBABBAABDBBAABABBAABBBABBBCAABBAABABABCAABBAABABCAABABABABABAAABBAABABABBAABAAABCAABAAABCAABCACACACBCACACBCAABBABABAABBAABABCAABCBABABCBABCAABCAABBACBBAABABBAABABABABABBBABABBAABBAABABBAABABBAABABABABABABBAABABAAABABABABBAABABABABBAABABABABAAABCAABABBABBBBBAABABABBAABBBBABAABABBABABABAABBAABBABAABAAABBAABABABBAABBABAABBABAABBABAABBAABBAABABBAABBAABBAABBAABBBBAABBAABABABBAABBAABBABAABBAABBAACBAABBABAABABABABBBBAABBBCACAABABCACAABCACACBCACAABCAABABCBABCAACCAACCAACCAACCACACACCCACACACACACAACCCACCACAEBCCACCACACACACACAACCACAACCACACBCACCCACACAABCACACAACACCCCACACACCCACACAAAABCBABABAAABABCAABBAABCAABABABBAABABBBCAABBAABABBBABBAABABCAABCAABABABBABABABABBAABABABABAACBBBABAABABBABAABBAABABBABBBCBBBAABABBAABBABABCBAABBBBBABBABABABABBBABAABBAABABABABBABABDBAAABAACBABAABABABBAABABBABBABABBAABBAABABBAABABCAABABBAABCAABCBABABABBAABABABABABABABCAABBAABABABABABABDBBDBBBABABABABABABAABBAABABBDCACAACCACACAABCAABABABCACAABEACAABABAAABCACAABABCBAAABCACBABCAABABBAABBAABABBAABBAABBABABAABABABBAABAAABCAABCACAABCACACACACAABCACAABCAABABABCAABABCAABCBABABABABADABABABCAABCACAABCAABCAABABCBABABBAABABABBAABBABAABABBBABBABAABCACAACCACAACCAACACCCCCACCACACACACAABCAACCAACCACACACAABCAABCAABCACAABCAAACACAACCACAACCCACBAACCAACACACCACAACCACBBBABCAABCBCBCBCBABCAABABABABABABABCAABCAABABCAABABABABABABABCAABBBABCAABCACAABABABABABCAABCAABCAABCAABABABCACACBCAABCAABCAABCBABABABABBAABBABAABABABABABCAABCAABABCACAABCAABCBABBAABBAABCAABCBBAABBAABBAABBAACBAABBAABBABAAABAABBCBABABAABBABABCBABAABBAABAABAABBABABAABCAABBABAACBAACBAACBAACACCAACCAACBAACBAABBAACBABACBABBAABBABABABAACBAACAAAABABAACAAACBABAACBABBBABAABABABCAABABBABAABBABABCBABABACBBBBABABABABAAAABABAAABBAABCAABBAABABABABBAACBAABABBABAABBAABBAACBABCBAABBAACBAABBABCBABABAACACBAACBABABAABBAAABAACBAABBCABBABAAABABAACBBBABABABABBBAACBAACACBAACBAACBAACBABABAACBABABABABAACBAACBABCACACACACACACBAACBCBCBABABABABABBACBAACACBCACACACACACACBAAABAACBABABABABCABBABAACACACACACACACBABABABABABABBBABCBCBCBCBABAABBBABABABABABABBABBBAABBABABAABBABBACBADEBCBABABABABAACBAACBCBCACBABCBABABCBCACBCACBAACBABABBBABABBBABABABABBBABABABABADBBABCBABAACBABABBBAABABBABABDBBBABADABAABBAABABCBCBABBABABAACBABADABABABAABBACBCBABABABABBABAABBACDABABBBBBABBBABBAABBAABBBBAABBABABBBABBBAABABABCBCAABABABBABBBABAABBABBBABABABBBBBBABBABABAABBAABBAABBAABBABAABABBBBABABABABA');
function Descompacta($string){
        $myArray = [];
        $str; 
        $str2; 
        $arr;
        $arr2;
        $lat;
        $lng; 
        $f;
        $strings = split(" ",$string);
        for($i=0; $i<count($strings); $i++)
        {
            $str = $strings[$i];
            $str2 = '';
            $arr = [];
            $arr2 = [];
            for ($j=0; $j<strlen($str); $j++)
            {
                switch (substr($str,$j))
                {
                    case 'A': $str2 += ',0'; break;
                    case 'B': $str2 += ',1'; break;
                    case 'C': $str2 += ',-1'; break;
                    case 'D': $str2 += ',2'; break;
                    case 'E': $str2 += ',-2'; break;
                    case 'F': $str2 += ',3'; break;
                    case 'G': $str2 += ',-3'; break;
                    case 'H': $str2 += ',4'; break;
                    case 'I': $str2 += ',-4'; break;
                    case 'J': $str2 += ',5'; break;
                    case 'K': $str2 += ',-5'; break;
                    case 'L': $str2 += ',6'; break;
                    case 'M': $str2 += ',-6'; break;
                    case 'N': $str2 += ',7'; break;
                    case 'O': $str2 += ',-7'; break;
                    case 'P': $str2 += ',8'; break;
                    case 'Q': $str2 += ',-8'; break;
                    case 'R': $str2 += ',9'; break;
                    case 'S': $str2 += ',-9'; break;
                    default: $str2 += substr($str,$j); break;
                }
            }
            $arr = split(",",$str2);
            $f = array_shift($arr);
            $lng = $arr[0]/$f;
            $lat = $arr[1]/$f;
            array_push($arr2,[$lat, $lng]);

            for ($j=2; $j<count($arr); $j+=2)

            {
                $lng += $arr[$j]/$f;
                $lat += $arr[$j+1]/$f;
                array_push($arr2,[$lat, $lng]);
            }
            array_push($myArray,$arr2);
        }
        }

The mistakes start here:

$arr = split(",",$str2);

Has anyone figured out where the mistake is?

1 answer

3

1st Problem

The problem you indicated is caused by using the wrong function for the desired purpose.

If you intend to share one string in array using a comma separator character, you need to use the function explode():

$arr = explode(",", $str2);

The function split() does the same as the function explode(), but the way they operate is different:

explode() - Divides the string in array using a string.

slit() - Divides the string in array using a regular expression.

2nd problem

The function substr() PHP, if you do not receive the 3 parameters will not return only one character as you want it to happen. Where you have:

substr($str,$j) // tudo depois da posição $j

You have to put:

substr($str, $j, 1) // um caractere após a posição $j

3rd problem

Concatenation in PHP is performed with the character ., in your switch() is using the character +, that is, where you have:

switch (substr($str,$j)) {
  case 'A': $str2 += ',0'; break;
  case 'B': $str2 += ',1'; break;
  case 'C': $str2 += ',-1'; break;
  case 'D': $str2 += ',2'; break;
  case 'E': $str2 += ',-2'; break;
  case 'F': $str2 += ',3'; break;
  case 'G': $str2 += ',-3'; break;
  case 'H': $str2 += ',4'; break;
  case 'I': $str2 += ',-4'; break;
  case 'J': $str2 += ',5'; break;
  case 'K': $str2 += ',-5'; break;
  case 'L': $str2 += ',6'; break;
  case 'M': $str2 += ',-6'; break;
  case 'N': $str2 += ',7'; break;
  case 'O': $str2 += ',-7'; break;
  case 'P': $str2 += ',8'; break;
  case 'Q': $str2 += ',-8'; break;
  case 'R': $str2 += ',9'; break;
  case 'S': $str2 += ',-9'; break;
  default: $str2 += substr($str,$j); break;
}

You should have:

switch (substr($str, $j, 1)) {
  case 'A': $str2 .= ',0'; break;
  case 'B': $str2 .= ',1'; break;
  case 'C': $str2 .= ',-1'; break;
  case 'D': $str2 .= ',2'; break;
  case 'E': $str2 .= ',-2'; break;
  case 'F': $str2 .= ',3'; break;
  case 'G': $str2 .= ',-3'; break;
  case 'H': $str2 .= ',4'; break;
  case 'I': $str2 .= ',-4'; break;
  case 'J': $str2 .= ',5'; break;
  case 'K': $str2 .= ',-5'; break;
  case 'L': $str2 .= ',6'; break;
  case 'M': $str2 .= ',-6'; break;
  case 'N': $str2 .= ',7'; break;
  case 'O': $str2 .= ',-7'; break;
  case 'P': $str2 .= ',8'; break;
  case 'Q': $str2 .= ',-8'; break;
  case 'R': $str2 .= ',9'; break;
  case 'S': $str2 .= ',-9'; break;
  default: $str2 .= substr($str, $j, 1); break;
}

4th problem

Your function is not giving back anything, that is, it is called, it performs its work and then "throws away" what it has been doing. At the end of it you must make use of the return to return the variable $myArray:

function Descompacta($string) {

  // ...

  return $myArray;
}

5th problem

Before dividing values for variables $lng and $lat, in Javascript, the integer value to use in the division is being extracted. In PHP you are not acting that way.

For this purpose, you can use the function intval() that will return the integer value of a variable:

$lng = $arr[0]/$f;
$lat = $arr[1]/$f;

Will pass to:

$lng = intval($arr[0])/$f;
$lat = intval($arr[1])/$f;

Example in Ideone

I used a string smaller input because Ideone does not provide enough memory to run the script the size of your string original:

function Descompacta($string) {

    $myArray = [];
    $str; 
    $str2; 
    $arr;
    $arr2;
    $lat;
    $lng; 
    $f;
    $strings = explode(" ",$string);

    for ($i=0; $i<count($strings); $i++) {

        $str = $strings[$i];
        $str2 = '';
        $arr = [];
        $arr2 = [];

        for ($j=0; $j<strlen($str); $j++) {

            switch (substr($str, $j, 1)) {

                case 'A': $str2 .= ',0';    break;
                case 'B': $str2 .= ',1';    break;
                case 'C': $str2 .= ',-1';   break;
                case 'D': $str2 .= ',2';    break;
                case 'E': $str2 .= ',-2';   break;
                case 'F': $str2 .= ',3';    break;
                case 'G': $str2 .= ',-3';   break;
                case 'H': $str2 .= ',4';    break;
                case 'I': $str2 .= ',-4';   break;
                case 'J': $str2 .= ',5';    break;
                case 'K': $str2 .= ',-5';   break;
                case 'L': $str2 .= ',6';    break;
                case 'M': $str2 .= ',-6';   break;
                case 'N': $str2 .= ',7';    break;
                case 'O': $str2 .= ',-7';   break;
                case 'P': $str2 .= ',8';    break;
                case 'Q': $str2 .= ',-8';   break;
                case 'R': $str2 .= ',9';    break;
                case 'S': $str2 .= ',-9';   break;
                default: $str2 .= substr($str, $j, 1);
            }
        }

        $arr = explode(",", $str2);
        $f = array_shift($arr);
        $lng = intval($arr[0])/$f;
        $lat = intval($arr[1])/$f;
        array_push($arr2,[$lat, $lng]);

        for ($j=2; $j<count($arr); $j+=2) {

            $lng += intval($arr[$j])/$f;
            $lat += intval($arr[$j+1])/$f;
            array_push($arr2,[$lat, $lng]);
        }

        array_push($myArray,$arr2);
    }

    return $myArray;
}

$output = Descompacta('1000I6575E3374BCBAACBABABABABBABBAABBBABBAABBABBDABAABBBABBAABBAABBABAABBBABBAABBBABBBBAABBAACBABCBABAABBABCBAACBCACB');

var_dump($output);

  • ,got it,but I think the error is when I quit switchcase because if I give print_r on $str2,it comes out as "INF"

  • That’s not what you meant in the question, I haven’t been analyzing everything. But in your switch() you’re concatenating with += but in PHP you have to use .= :)

  • @Rodolfooliveira I was reviewing the function, solved more problems I found and set an example in Ideone. Read the answer so you know where I found problems and why they happened.

  • 1

    It seems that you are right, but I noticed that in the examples the intval (to be equivalent to parseInt), no? I haven’t tested the code and I really don’t know how it works, but I believe this is part of the important.

  • @Guilhermenascimento Thanks, I had not seen this detail. I did not understand yet if it is required or if it already came from the original in JS, but it is there :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.