Location: PHPKode > scripts > Experts Exchange Fetcher > experts-exchange-fetcher/expert_class.php
<?php
/*
 This Class is licensed under GNU GPL;
 You can modify,distribute,use but just don't remove this author and copyright information
 Copyright Abhinaba Basu
 hide@address.com
 You can contact me if you need any assistance regarding this class
*/
class expert_class {
public $question_title; // Title of the best mtched question
public $question_body; //Full body of the question(without snippet)
public $questioncodesnippet; //Question code snippet
public $ans_body; //Full body of the accepted solution(without snippet)
public $anscodesnippet; //Answer code snippet
public $query;
public $bestgooglematch=""; //Store the google cache url

 
public function __construct($eequery) {
    	$this->query = $eequery;
	$this->getgooglecache();
	$this->returnexpertcomment();
}


private function TextBetweenArray($s1,$s2,$s){
  $myarray=array();
  $s1=strtolower($s1);
  $s2=strtolower($s2);
  $L1=strlen($s1);
  $L2=strlen($s2);
  $scheck=strtolower($s);
 
  do{
  $pos1 = strpos($scheck,$s1);
  if($pos1!==false){
    $pos2 = strpos(substr($scheck,$pos1+$L1),$s2);
    if($pos2!==false){
      $myarray[]=substr($s,$pos1+$L1,$pos2);
      $s=substr($s,$pos1+$L1+$pos2+$L2);
      $scheck=strtolower($s);
      }
        }
  } while (($pos1!==false)and($pos2!==false));
  return $myarray;
}


/*
Search the google and get the best possible match. 
After that it get the cache url
*/
private function getgooglecache()
{
	$result="";
	$handle2 = fopen("http://www.google.co.in/search?hl=en&client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&hs=ns&q=".$this->query."+site%3Aexperts-exchange.com&btnG=Search&meta=", 'r');
	while (!feof($handle2))  // Loop through the site until we get an "End of File" marker.
	{
      	$result .= fread($handle2, 8192); // Add the data to the $result variable
	}
	list($this->bestgooglematch) = $this->TextBetweenArray('</cite><span class=gl><a href="', '">Cached</a>', $result);
	$result="";//Empty the result
}

/*
Function grab the question title, question main body and accepted solution.
*/
private function returnexpertcomment()
{
	$result="";
	$handle = fopen($this->bestgooglematch, 'r');
	while (!feof($handle))  // Loop through the site until we get an "End of File" marker.
	{
      	$result .= fread($handle, 8192); // Add the data to the $result variable
	}
	list($besteeansdivmatch) = $this->TextBetweenArray('answer acceptedHeader"><table class="h">', 'ewWindow"', $result);
	list($this->question_title) = $this->TextBetweenArray('<h1 class="titleHeader">', '</h1>', $result);
	$this->question_title=strip_tags($this->question_title); //Remove all html tags
	list($question_mainwithcodesnippet) = $this->TextBetweenArray('<div class="qBody" id="EchoTopic">', '<div class="qStats">', $result);
	list($this->question_body,$this->questioncodesnippet)=explode('<td class="codeBody">',$question_mainwithcodesnippet);
	list($this->question_body,$snipline)=explode('<td class="lineNumbers">',$this->question_body); //remove snippet lines
	$this->question_body=strip_tags($this->question_body); //Remove all html tags
	$this->questioncodesnippet=strip_tags($this->questioncodesnippet); //Remove all html tags
	list($getanswithcodesnippet)=$this->TextBetweenArray('<div class="answerBody quoted" id="EchoTopic">','<a class="openInN',$besteeansdivmatch);
	list($this->ans_body,$this->anscodesnippet)=explode('<td class="codeBody">',$besteeansdivmatch);
	list($this->ans_body,$snipline)=explode('<td class="lineNumbers">',$this->ans_body); //remove snippet lines
	list($author,$this->ans_body)=explode('<div class="richText">',$this->ans_body); //remove author information
	$this->ans_body=strip_tags($this->ans_body); //Remove all html tags
	$this->anscodesnippet=strip_tags($this->anscodesnippet); //Remove all html tags
}
}
?>
Return current item: Experts Exchange Fetcher