在域名上查找单词组合

时间:2015-05-08 19:02:21

标签: php text-extraction domain-name keyword-search

我是一名PHP新手,需要一些帮助来完成我的脚本。我有一个PHP脚本,可以从域名中获取所有单词。我需要脚本才能找到最可能的域名关键词。

这是我的剧本:

<?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 ) );

?>

代码输出:

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 )

我想取数组并找到没有任何字重叠的单词组合,以确定域名关键字。

任何人都知道如何做到这一点?我知道我需要遍历这些单词并将它们与原始域进行核对,但这似乎有点过头了。

1 个答案:

答案 0 :(得分:0)

首先,您必须从数组中取消设置空值。 第二个没有任何感觉的所有字母都没有设置。 然后尝试我的代码:

<?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] =&gt; string(2)“to”[1] =&gt; string(2)“go”[2] =&gt;串(2)   “to”[3] =&gt; string(5)“going”[4] =&gt; string(2)“mi”[5] =&gt; string(1)“a”   [6] =&GT; string(5)“where”}

了解逆转顺序。