PHP Classes
Icontem

File: Highlighter.php


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Nguyen Duc Thuan  >  Fete Search Highlighter  >  Highlighter.php  
File: Highlighter.php
Role: Class source
Content type: text/plain
Description: Main class
Class: Fete Search Highlighter
Highlight keywords within HTML
 

Contents

Class file image Download
<?php

/* 
 * Copyright (c) 2009 Nguyen Duc Thuan <me@ndthuan.com>
 * All rights reserved.
 */

/**
 * Text highlighter without affecting HTML tags. This class supports highlighting
 * string in the double quotes of keyword for compatibility with some search methods such as
 * MySQL Full-text search, Google, Yahoo...
 *
 * Sample usage:
 * - Method 1:
 *      $highlightedString = Fete_Util_Text_Highlighter::createInstance('<b>', '</b>')->highlight('PHP rules', 'rules');
 * - Method 2:
 *      $highlighter = new Fete_Util_Text_Highlighter();
 *      $highlightedString = $highlighter->setBeforeMatch('<b>')
 *                  ->setAfterMatch('</b>')
 *                  ->highlight('PHP rules the world', '"PHP rules"');
 *
 * @author Nguyen Duc Thuan <me@ndthuan.com>
 */
class Fete_Util_Text_Highlighter
{
    
/**
     * @var string
     */
    
protected $_beforeMatch '<span style="background-color:yellow">';

    
/**
     * @var string
     */
    
protected $_afterMatch '</span>';

    
/**
     *
     * @param string $beforeMatch
     * @param string $afterMatch
     */
    
public function __construct($beforeMatch null$afterMatch null)
    {
        if (
null !== $beforeMatch) {
            
$this->_beforeMatch $beforeMatch;
        }

        if (
null !== $afterMatch) {
            
$this->_afterMatch $afterMatch;
        }
    }

    
/**
     *
     * @param string $beforeMatch
     * @param string $afterMatch
     * @return Fete_Util_Text_Highlighter
     */
    
static public function createInstance($beforeMatch null$afterMatch null)
    {
        return new 
self($beforeMatch$afterMatch);
    }

    
/**
     *
     * @param string $beforeMatch
     * @return Fete_Util_Text_Highlighter
     */
    
public function &setBeforeMatch($beforeMatch)
    {
        
$this->_beforeMatch $beforeMatch;
        return 
$this;
    }

    
/**
     *
     * @param string $afterMatch
     * @return Fete_Util_Text_Highlighter
     */
    
public function &setAfterMatch($afterMatch)
    {
        
$this->_afterMatch $afterMatch;
        return 
$this;
    }

    
/**
     *
     * @param string $text
     * @param string $keyword
     * @return string highlighted string
     */
    
public function highlight($text$keyword)
    {
        
$output '';

        
$words = array();

        
preg_match_all('#(?:"([^"]+)"|(?:[^\s\+\-"\(\)><~\*\'\|\\`\!@\#\$%^&_=\[\]\{\}:;,\./\?]+))#si'$keyword$matchesPREG_SET_ORDER);

        foreach (
$matches as $match)
        {
            if (
=== count($match)) {
                
$words[] = $match[1];
            } else {
                
$words[] = $match[0];
            }
        }

        
$words implode('|'$words);

        
$textParts preg_split('#(<script[^>]*>.*?</script>|<style[^>]*>.*?</style>|<.+?>)#si'$text, -1PREG_SPLIT_DELIM_CAPTURE);

        foreach (
$textParts as $byHtmlPart)
        {
            if (!empty(
$byHtmlPart) && $byHtmlPart{0} != '<') {
                
$byHtmlPart preg_replace('#(' $words ')#si'$this->_beforeMatch '\1' $this->_afterMatch$byHtmlPart);
            }

            
$output .= $byHtmlPart;
        }

        return 
$output;
    }
}

 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products