<?php
/* Content Create
 * (C) NuCode 2009
 * Licensed under Creative Commons (CC) BY-SA 3.0
 * http://creativecommons.org/licenses/by-sa/3.0/
 *
 *
 * EDUCATIONAL PURPOSES ONLY. This script is meant to demonstrate coherence of
 * simplistic shuffling method to create "unique" articles of "content"
 * Using this on  your website you risk your website's reputation and might be
 * marked as a spammer. Use this only for research purposes.
 *
 * To execute normally on *nix shell, add to the beginning: #!/bin/php
 * Make sure the path is to PHP binary. Change file to executable with chmod o+x
*/

if(!empty($argv[1])){
    
// Build the keywords list
    
$keywords $argv;
    unset(
$keywords[0]); // Take out the command name. You might need to remove index 1
        // aswell if running like php contentCreate.php keywords, and above !empty if-clause
    
$keyword implode(' '$keywords); // Not everyone knows you can use "" so this is simpler
        // Yet you can still use "" :)

    // Get the feed. Changing this url could allow you use other search engines.
    
$xmlContent file_get_contents('http://blogsearch.google.com/blogsearch_feeds?hl=en&q=' .
        
urlencode($keyword) . '&ie=utf-8&num=10&output=rss');
    
$domDoc = new DOMDocument();
    
$domDoc->loadXML($xmlContent);
    
    
$items $domDoc->getElementsByTagName('item'); // Take out items out of the feed

    
$contentString ''// This is our final content variable being declared.
    
    
foreach($items as $item){ // Iterate through the items
        
$description $item->getElementsByTagName('description'); // Get description
        
        
$contentString .= preg_replace('/[^\w\s\.]/'''strip_tags($description->item(0)->nodeValue) );
    }
    
    
    
$arraySentences preg_split('/[\.\?\!\:]/'$contentString, -1PREG_SPLIT_NO_EMPTY); // Parse out sentences
    
foreach($arraySentences as $key => $sentence){
            
// Use multibyte strlen if function exists. This is so if the content is UTF-8
        
if (function_exists('mb_strlen')) $sentenceLength mb_strlen($sentence);
            else 
$sentenceLength strlen($sentence);
            
        if(
$sentenceLength 20// Don't accept under 20 char sentences
            
unset($arraySentences[$key]);
        else 
$arraySentences[$key] = trim$arraySentences[$key] ); // Strip whitespace
    
}
    
    
shuffle($arraySentences); // Mix it up a little bit. Target is to have coherent simple sentences, but
        // Change up the content, so it seems unique
    
    
echo implode('. '$arrSentences).".\n"// And combine into a single array.
    
} else {
    
// No keywords passed, show usage help
    
echo "Usage: {$argv[0]} keywords\n";
    echo 
"Examples:\n {$argv[0]} php\n";
    echo 
"{$argv[0]} php library nucode\n";
}