4

I am a PHP novice and need some help finishing my script. I have a PHP script that can take all of the words from a domain name. I need the script to be able to find the most likely words that are the domain name's keywords.

Here is my script:

<?php

$domain = trim(htmlspecialchars('where-amigoing.togoto.com'));
preg_match('/(.*?)((\.co)?.[a-z]{2,4})$/i', $domain, $m);
$ext = isset($m[2]) ? $m[2]: '';

$replace = array($ext,'-','.');
$domainWords = str_replace($replace,'',$domain);

//Find Word in Dictionary
function pspell_icheck($dictionary_link, $word) {
  return ( pspell_check($dictionary_link, $word) ||
    strtolower(reset(pspell_suggest($dictionary_link, $word))) == strtolower($word) );
}

//Find Words
function getwords( $string ) {
    if( strpos($string,"xn--") !== false ) {
        return false;
    }
    $pspell = pspell_new( 'en' );
    $check = 0;
    $words = array();
    for( $j = 0; $j < ( strlen( $string ) ); $j++ ) {
        for( $i = 0; $i < strlen( $string ); $i++ ) {
            if( pspell_icheck( $pspell, substr( $string, $j, $i ) ) ) {
                $check++;
                $words[] = substr( $string, $j, $i );
            }
        }
    }
    $words = array_unique( $words );
    if( $check > 0 ) {
        return $words;
    }
    return false;
}

echo 'domain name: '.$domain .'<br>';
echo 'domain words: '.$domainWords .'<br>';
echo 'domain extension: '.$ext .'<br>';
print_r ( getWords( $domainWords ) );

?>

The code outputs this:

domain name: where-amigoing.togoto.com

domain words: whereamigoingtogoto

domain extension: .com

Array ( [0] => [1] => w [2] => where [4] => h [5] => he [6] => her [7] => here [9] => e [10] => er [11] => ere [13] => r [14] => re [15] => rea [16] => ream [19] => ea [21] => a [22] => am [23] => ami [24] => amigo [26] => m [27] => mi [28] => mig [30] => i [32] => g [33] => go [34] => going [36] => o [37] => oi [40] => in [42] => n [45] => gt [47] => t [48] => to [49] => tog [50] => togo [56] => got [59] => ot )

I want to take the array and find the word combinations without any word overlapping, to determine the domain name keywords.

Anyone know how to do this? I know I need to loop through the words and check them against the original domain, but this seems a little over my head.

Paulo Freitas
  • 13,194
  • 14
  • 74
  • 96
Daniel
  • 293
  • 4
  • 10

1 Answers1

0

First of all you must unset blank value from array. Second unset all letter without any sens as word. Then try my code:

<?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';
}

Output:

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]=> string(5) "where" }

Lookout for revers order.

Kuguar6
  • 26
  • 1
  • 5