, .
- .
:
<?php
class domainWordsCutter
{
private $words;
private $wordsArray = array();
public function __construct($words)
{
$this->words = $words;
}
public function cutWords($domainWords)
{
if(empty($domainWords))
{
return true;
}
foreach($this->words as $word)
{
$wordLen = strlen($word);
if
(
$wordLen <= strlen($domainWords) &&
substr($domainWords, 0, $wordLen) == $word &&
$this->cutWords(substr($domainWords, $wordLen))
)
{
$this->wordsArray[] = $word;
return true;
}
}
return false;
}
public function getWordsArray()
{
return $this->wordsArray;
}
}
$domainWordsCutter = new domainWordsCutter(array ( 2 => 'where', 5 => 'he', 6 => 'her', 7 => 'here', 10 => 'er', 11 => 'ere', 14 => 're', 15 => 'rea', 16 => 'ream', 19 => 'ea', 21 => 'a', 22 => 'am', 23 => 'ami', 24 => 'amigo', 27 => 'mi', 28 => 'mig', 30 => 'i', 33 => 'go', 34 => 'going', 37 => 'oi', 40 => 'in', 45 => 'gt', 48 => 'to', 49 => 'tog', 50 => 'togo', 56 => 'got', 59 => 'ot', ));
if($domainWordsCutter->cutWords('whereamigoingtogoto'))
{
var_dump($domainWordsCutter->getWordsArray());
}
else
{
echo 'Not found';
}
:
array (7) {[0] = > string (2) "to" [1] = > string (2) "go" [2] = > string (2) "to" [3] = > string (5) "going" [4] = > string (2) "mi" [5] = > string (1) "a" [6] = > (5) "where" }
.