Hello. I am taking input from a <textarea> and trying to create an object using input from each line in the textarea. So far I have foreach(array as row_input) objectarray = new Class(row_input) But, something keeps messing up. I can post the full code need be, but if theres a general solution when it comes to using foreach to create objects accepting each line from a textarea that'd be great! I guess, the objects are being created successfully, but the results from scraping Google are not accurate...That's the main issue. Thanks
Here is the code that explodes the input from a <textarea> to create an array that i can iterate through and create an object for each line. <?php $seperated_keys = explode("\n", $keys); $i = 0; $array = array(); foreach($seperated_keys as $row){ $array[$i] = new Scraper("$site", $row); $i++; } ?> PHP: Here is the class that is being instantiated <?php class Scraper{ protected $domain, $keywords, $singleKeyword; public function __construct($website, $wordarray){ $this->domain = $website; $this->keywords = $wordarray; $this->scrape(); } //google needs plus signs in the keyword, so were adding them with this public function addSymbols(){ $singleWords = explode(" ", $this->keywords); $i=0; $a=""; $found=""; reset ($singleWords) ; foreach($singleWords as $line){ $a .= $singleWords[$i]."+"; $i++; } $this->singleKeyword = $a; } //while an iterator is less than 20, it scrapes google results with 1 result, checks if the site is there, if not then scrapes with 2 results, etc function scrape(){ $this->addSymbols(); $iterator = 1; $pattern = '/'.$this->domain.'/'; while ($iterator < 20){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://www.google.com/search?hl=en&pws=0&num=$iterator&q=".$this->singleKeyword ); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); if (preg_match($pattern, $output)){ echo $this->domain . " found at position $iterator for".$this->keywords." <br/>"; $iterator = 1; $found = 1; break; } else{ $found = 0; } $iterator++; }//end the loop scanning results if($found==0) echo $this->domain . " is not in the top 20 for".$this->keywords."<br/>"; $iterator = 1; $this->cleanUp(); } //this takes up space function cleanUp(){ } } ?> PHP:
Nasty code you wrote. Two things 2 say : 1.You don't know what foreach does.... 2.You do know that keywords can be composed of multiple words and not just one word right ? Here is the proper class you wanted : class Scraper{ protected $domain, $keywords, $singleKeyword; public function __construct($website, $wordarray){ $this->domain = $website; $this->keywords = $wordarray; $this->scrape(); } public function addSymbols(){ $singleWords = explode(",", $this->keywords); $a=""; if(is_array($singleWords) && count($singleWords) > 0) { foreach($singleWords as $line){ $a .= '+'.$line; } } else { $a = '+'.$singleWords; } $this->singleKeyword = $a; } function scrape(){ $this->addSymbols(); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://www.google.com/search?hl=en&pws=0&num=20&q=".$this->singleKeyword ); curl_setopt($ch, CURLOPT_USERAGENT,'Googlebot/2.1 (http://www.googlebot.com/bot.html)'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); $found = 0; $dom = new DOMDocument(); @$dom->loadHTML($output); $xpath = new DOMXPath($dom); $hrefs = $xpath->evaluate("//div[@id='ires']/ol/li/h3[@class='r']/a"); for ($i = 0; $i < $hrefs->length; $i++) { $href = $hrefs->item($i); $url = $href->getAttribute('href'); if(strpos($url,$this->domain) !== false) { $found=1; echo $this->domain." found at position ".$i." for ".$this->keywords." <br/>"; break; } } if($found==0) echo $this->domain . " is not in the top 20 for".$this->keywords."<br/>"; } function cleanUp(){ } } PHP:
Use this code: <?php $keys = 'google\nyahoo\nfacebook\n'; $site = 'yahoo.com'; $array = array(); require_once('scrapper.php'); foreach(explode('\n', $keys) as $row) { if(!empty($row)) { $array[] = new Scraper($site, $row); } } echo '<pre>'; print_r($array); echo '</pre>'; ?> Code (markup): use the scrapper class provided by tvoodoo It will work
Thanks! I'll try this out. This was actually the first object oriented thing I've tried in PHP so I appreciate the patience.