<?php
/************************************************************************************************
FILE : highlight-keyword.class.php
VERSION : 1.0
AUTHOR : Kunjesh J Sukhadia
E-Mail : hide@address.com
last update : 23 October 2008
Note : I had already uplode fetch-curl-data.class.php in my earlier class please collect from that class.
************************************************************************************************/
/***********************************************************************************************
* highlight-keyword.class.php
**********************************************************************************************
* 1). This Class will highlight all the keywords from html page without effecting any html special tags (eg. html,body,img etc.).
* If any keyword is define as property in html that will also not highlighted (eg. <div class="myClass">myClass</div>) so
* only myClass which is between in div will be highlighted and myClass which is define as class of name will not be highlighted.
*
* 2). Unlike google not scrolling the page when the first keyword is found in html body. This class will do the same thing also.
*************************************************************************************************/
require_once('fetch-curl-data.class.php'); // Include fetch curl data class.
class Classes_preview_iframe_data
{
public $mCurlData = '';
public $mUrl = '';
public $mHtmlPettern = '';
public $mBaseUrl = '';
public $mBgColorCode = array();
public $mFColorCode = array();
public $mHtmlTags = array();
public $mHtmlAttributes = array();
public $mHtmlHeadEndTag = '</head>';
// Cunstructor of preview iframe data class.
function __construct()
{
$this->mHtmlPettern = '<head>';
$this->mBgColorCode = array('ffff66','99ff99','ff9999','ff66ff','880000','00aa00','886800','004699','990099');
$this->mFColorCode = array('000000','FFFFFF');
}
// This function will fetch html data from curl through fetch curl data class.
// This is main function you have to just pass url from where you got html and keyword those are you want to highlight.
function FetchCurlData($Url,$keyword)
{
$curl_obj = new Classes_fetch_curl_data(); // Object of fetch curl data class.
$this->mUrl = $Url;
// page_content
$page_content = $curl_obj->GetCurlData($this->mUrl);
$get_processed_data = $this->ProcessCurlData($page_content,$keyword);
return $get_processed_data;
}
// This function will process curl data ex(append base url tag in html content).
// This function is call from FetchCurlData function. This function is only required html string and keyword which you want to highlight.
function ProcessCurlData($curlData,$keyword)
{
//These many lines of code is used for adding <base> tag in existing html tag.
//--------------------------------------------------------------------------------------//
$str_lenght = strlen($this->mHtmlPettern);
$str_position = strpos($curlData,$this->mHtmlPettern);
$str_actual_position = $str_position + $str_lenght;
$first_part = substr($curlData,0,$str_actual_position);
$last_part = substr($curlData,$str_actual_position);
$this->mBaseUrl = '<base href='.$this->mUrl.' />';
$full_page_content = $first_part.$this->mBaseUrl.$last_part;
$full_page_content_1 = $full_page_content; // Get full html with base tag.
$main_containts = $full_page_content_1;
//--------------------------------------------------------------------------------------//
$bgcolor = 0;
$fcolor = 0;
// This code is used to add bookmark first keyword in html.
//--------------------------------------------------------------------------------------//
$add_bookmark = '<a name=first_keyword>'.$keyword[0].'</a>';
$main_containts = preg_replace("/(>|^)([^<]+)(?=<|$)/esx","'\\1' . str_replace('" . $keyword[0] . "', '" . $add_bookmark . "', '\\2')",$main_containts);
//--------------------------------------------------------------------------------------//
// This code is used for highlighting keywords in html with different colors likely google used. Total 8 colors
// are used, if keywords are more than 8 than colors will repeted.
//--------------------------------------------------------------------------------------//
for($i = 0; $i < count($keyword); $i++)
{
// If keyword length is more than 3 character than and than only it will highlight.
//---------------------------------------------------------------------------------------//
if(strlen($keyword[$i]) >= 3)
{
$keyword[$i] = htmlentities($keyword[$i],ENT_QUOTES);
if($bgcolor == 8)
{
$bgcolor = 0;
$fcolor = 0;
}
if($bgcolor == 3)
{
$fcolor = $fcolor + 1;
}
$add_span = '<span style=background-color:#'.$this->mBgColorCode[$bgcolor].';color:#'.$this->mFColorCode[$fcolor].';font-weight:bold;>'.$keyword[$i].'</span>';
$main_containts = preg_replace("/(>|^)([^<]+)(?=<|$)/esx","'\\1' . str_replace('" . $keyword[$i] . "', '" . $add_span . "', '\\2')",$main_containts);
$bgcolor = $bgcolor + 1;
}
//---------------------------------------------------------------------------------------//
}
//--------------------------------------------------------------------------------------//
return $main_containts;
}
}
?>