Script from Js to php

Hi everybody,
Can anybody convert the below js script to php?
Thank you.

--------------------------------
var score = 0;
   
    // award every unique letter until 5 repetitions
    var letters = new Object();
    for (var i=0; i<pass.length; i++) {
        letters[pass[i]] = (letters[pass[i]] || 0) + 1;
        score += 5.0 / letters[pass[i]];
    }

    // bonus points for mixing it up
    var variations = {
        digits: /\d/.test(pass),
        lower: /[a-z]/.test(pass),
        upper: /[A-Z]/.test(pass),
        nonWords: /\W/.test(pass),
    }

    variationCount = 0;
    for (var check in variations) {
        variationCount += (variations[check] == true) ? 1 : 0;
    }
    score += (variationCount - 1) * 10;

Open in new window

LVL 1
myyisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
This may not be exactly right, but it's probably close enough to start your tests.  As with any programming task, the quality of the outcome is highly dependent on the test data set!

Please see: http://www.laprbass.com/RAY_temp_myyis.php

<?php // RAY_temp_myyis.php
error_reporting(E_ALL);
echo '<pre>';

/** http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28271917.html
var score = 0;

    // award every unique letter until 5 repetitions
    var letters = new Object();
    for (var i=0; i<pass.length; i++) {
        letters[pass[i]] = (letters[pass[i]] || 0) + 1;
        score += 5.0 / letters[pass[i]];
    }

    // bonus points for mixing it up
    var variations = {
        digits: /\d/.test(pass),
        lower: /[a-z]/.test(pass),
        upper: /[A-Z]/.test(pass),
        nonWords: /\W/.test(pass),
    }

    variationCount = 0;
    for (var check in variations) {
        variationCount += (variations[check] == true) ? 1 : 0;
    }
    score += (variationCount - 1) * 10;
*/

$tests = array
( 'fimlgooseball'
, 'AAAAAAAAAAAAA123'
, 'pigmama'
, 'letmein'
, 'password'
, 'abc123'
, 'Z5%$?Qut=42'
, 'A'
, 'AA'
, 'AAA'
, 'Aa'
, 'A1#a'
, 'qwertyuiopasdfghjklzxcvbnm'
, 'QwErTyUiOpAsDfGhJkLzXcVbNm<.?1!2@3#4e$5%6^7&8*9(0)+='
)
;

// MASTER REGEX LIST
$master = array
( 'digits' => '/\d/'
, 'lower'  => '/[a-z]/'
, 'upper'  => '/[A-Z]/'
, 'Xwords' => '/[^A-Z 0-9]/i'
)
;

// WITH EACH TEST CASE
foreach ($tests as $word)
{
    $score = 0;
    $regex = $master;

    // MAKE AN ARRAY TO HOLD THE LETTER COUNTS
    $pass = array();
    $ltrs = str_split($word);
    foreach ($ltrs as $ltr)
    {
        // GET LETTER COUNTS
        $pass[$ltr] = isset($pass[$ltr]) ? $pass[$ltr]++ : 1;

        // LIMITED TO FIVE
        $pass[$ltr] = ($pass[$ltr] > 5) ? 5 : $pass[$ltr];
    }

    // CHECK EACH LETTER WITH THE REGEX ONLY ONCE
    foreach ($pass as $ltr => $num)
    {
        foreach ($regex as $key => $expr)
        {
            // IF A MATCH, ADD TO COUNT AND REMOVE REGEX FROM FURTHER USE
            if (preg_match($expr, $ltr))
            {
                $score++;
                unset($regex[$key]);
            }
        }
    }

    // COUNT (MINUS ONE) IS WORTH 10X
    $score--;
    $score = $score * 10;

    // SUM OF LETTER COUNTS
    foreach ($pass as $ltr => $num)
    {
        $score = $score + $num;
    }

    // SHOW THE WORD AND THE SCORE
    echo PHP_EOL . "$word => $score";
}

Open in new window

Best regards, ~Ray
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
myyisAuthor Commented:
Thank you
0
Ray PaseurCommented:
Thanks for the points and thanks for using EE, ~Ray
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.