title: Using php to extract keyword pairs for SEO
body: <p>I'm currently investigating some new ideas for long tail SEO. I have a site where people can create their own blogs, which brings pretty good long tail traffic already. I'm already displaying the article title inside the article's title tags.</p>
  
  <p>However, often the title does not match well for keywords in the content, and I'm interested in maybe adding some keywords into the title that php has actually determined would be best.</p>
  
  <p>I've tried using a script which I made to work out what the most common words are on a page. This works ok but the problem with this is it comes up with pretty useless words. </p>
  
  <p>It's occurred to me that what would be useful is to make a php script that would extract frequently occurring pairs (or sets of 3) words and then put them in an array ordered by how often they occur.</p>
  
  <p><strong>My problem:</strong> how to parse text in a more dynamic way to look for recurring pairs or triplets of words. How would I go about this?</p>
  
  <pre><code>function extractCommonWords($string, $keywords){
    $stopWords = array('i','a','about','an','and','are','as','at','be','by','com','de','en','for','from','how','in','is','it','la','of','on','or','that','the','this','to','was','what','when','where','who','will','with','und','the','www');
  
    $string = preg_replace('/\s\s+/i', '', $string); // replace whitespace
    $string = trim($string); // trim the string
    $string = preg_replace('/[^a-zA-Z0-9 -]/', '', $string); // only take alphanumerical characters, but keep the spaces and dashes too…
    $string = strtolower($string); // make it lowercase
  
    preg_match_all('/\b.*?\b/i', $string, $matchWords);
    $matchWords = $matchWords[0];
  
    foreach ( $matchWords as $key=&gt;$item ) {
        if ( $item == '' || in_array(strtolower($item), $stopWords) || strlen($item) &lt;= 3 ) {
            unset($matchWords[$key]);
        }
    }   
    $wordCountArr = array();
    if ( is_array($matchWords) ) {
        foreach ( $matchWords as $key =&gt; $val ) {
            $val = strtolower($val);
            if ( isset($wordCountArr[$val]) ) {
                $wordCountArr[$val]++;
            } else {
                $wordCountArr[$val] = 1;
            }
        }
    }
    arsort($wordCountArr);
    $wordCountArr = array_slice($wordCountArr, 0, $keywords);
    return $wordCountArr;
  }
  </code></pre>