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.119 tracks/php/exercises/word-count/example.php
trackler-2.2.1.118 tracks/php/exercises/word-count/example.php
trackler-2.2.1.117 tracks/php/exercises/word-count/example.php
trackler-2.2.1.116 tracks/php/exercises/word-count/example.php
trackler-2.2.1.115 tracks/php/exercises/word-count/example.php
trackler-2.2.1.114 tracks/php/exercises/word-count/example.php
trackler-2.2.1.113 tracks/php/exercises/word-count/example.php
trackler-2.2.1.111 tracks/php/exercises/word-count/example.php
trackler-2.2.1.110 tracks/php/exercises/word-count/example.php
trackler-2.2.1.109 tracks/php/exercises/word-count/example.php
trackler-2.2.1.108 tracks/php/exercises/word-count/example.php
trackler-2.2.1.107 tracks/php/exercises/word-count/example.php
trackler-2.2.1.106 tracks/php/exercises/word-count/example.php
trackler-2.2.1.105 tracks/php/exercises/word-count/example.php
trackler-2.2.1.104 tracks/php/exercises/word-count/example.php
trackler-2.2.1.103 tracks/php/exercises/word-count/example.php
trackler-2.2.1.102 tracks/php/exercises/word-count/example.php
trackler-2.2.1.101 tracks/php/exercises/word-count/example.php
trackler-2.2.1.100 tracks/php/exercises/word-count/example.php
trackler-2.2.1.99 tracks/php/exercises/word-count/example.php