Sha256: 8364f89a807e2634a643bdc0dbb7a28a0f3d879af175cfc2763d92a1bc59b69d

Contents?: true

Size: 407 Bytes

Versions: 396

Compression:

Stored size: 407 Bytes

Contents

<?php

function wordCount($phrase)
{
    $count = [];

    $phrase = preg_replace(['/[^a-zA-Z0-9]+/', '/\s\s/'], ' ', mb_strtolower($phrase));

    foreach (explode(' ', $phrase) as $word) {
        if (!array_key_exists($word, $count) && !empty($word)) {
            $count[$word] = 1;
        } elseif (array_key_exists($word, $count)) {
            $count[$word]++;
        }
    }

    return $count;
}

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/php/exercises/word-count/example.php
trackler-2.2.1.179 tracks/php/exercises/word-count/example.php
trackler-2.2.1.178 tracks/php/exercises/word-count/example.php
trackler-2.2.1.177 tracks/php/exercises/word-count/example.php
trackler-2.2.1.176 tracks/php/exercises/word-count/example.php
trackler-2.2.1.175 tracks/php/exercises/word-count/example.php
trackler-2.2.1.174 tracks/php/exercises/word-count/example.php
trackler-2.2.1.173 tracks/php/exercises/word-count/example.php
trackler-2.2.1.172 tracks/php/exercises/word-count/example.php
trackler-2.2.1.171 tracks/php/exercises/word-count/example.php
trackler-2.2.1.170 tracks/php/exercises/word-count/example.php
trackler-2.2.1.169 tracks/php/exercises/word-count/example.php
trackler-2.2.1.167 tracks/php/exercises/word-count/example.php
trackler-2.2.1.166 tracks/php/exercises/word-count/example.php
trackler-2.2.1.165 tracks/php/exercises/word-count/example.php
trackler-2.2.1.164 tracks/php/exercises/word-count/example.php
trackler-2.2.1.163 tracks/php/exercises/word-count/example.php
trackler-2.2.1.162 tracks/php/exercises/word-count/example.php
trackler-2.2.1.161 tracks/php/exercises/word-count/example.php
trackler-2.2.1.160 tracks/php/exercises/word-count/example.php