Monthly Archives: January 2015

Text tricks

Have you ever need to parse the text into the words? You can use the following code:

$result = preg_split('/((^\p{P}+)|(\p{P}*\s+\p{P}*)|(\p{P}+$))/', $text, -1, PREG_SPLIT_NO_EMPTY);

And a task which based on previous code:
Find the frequency of defined word anagrams in the whole text.


Some improvements, you can read text and defined word from command line.
The final code:

if (count($argv) < 2) {
    die('You should specify the file and the word');
}

$text = file_get_contents($argv[1]);
$word = trim($argv[2]);

$dict = array();

$sort = function($string) {
    $stringParts = str_split($string);
    sort($stringParts);
    return implode('', $stringParts);
};

$result = preg_split('/((^\p{P}+)|(\p{P}*\s+\p{P}*)|(\p{P}+$))/', $text, -1, PREG_SPLIT_NO_EMPTY);

if (count($result) > 0) {
    foreach($result as $value) {
        $dict[$sort($value)]++;
    }
    
    //Print frequency
    echo $dict[$sort($word)];
} else {
    echo 'Text is empty';
}