PHP Classes
elePHPant
Icontem

File: Apriori.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Dmitry Basileff  >  Apriori  >  Apriori.php  >  Download  
File: Apriori.php
Role: Class source
Content type: text/plain
Description: Main Class Apriori
Class: Apriori
Process text based on rules stored in MySQL table
Author: By
Last change: Added new property of Parslet - Type of RegEx.
Supported Types: POSIX Basic, POSIX Extended, Emacs, grep, GNU regex, Java, Perl, Ruby.
Date: 7 years ago
Size: 2,291 bytes
 

Contents

Class file image Download
<?php
#########################################
# Class Apriori 0.3.000DE #
# Modified part of VXi Web Platform #
# RusBreathLess, 2007 #
# basileff@gmail.com #
# http://rusbreathless.livejournal.com #
#########################################

// Security check
//if(!eregi("Apriori.php",implode(";",get_included_files()))){ exit;}

// Change this string, insert your favorite db abstraction layer :-)

mysql_connect("localhost","root");
mysql_select_db("Test");

/*
Data structure:

    Matcher = Pattern for search.
    Replacer = Code, that evaluate and must set var $Output; Pattern text will be replaced by this variable.
    Dynamic - parameter, which define, evaluate Replacer's code on&#1091; time, or for every founded pattern.
    Mode - parameter, which sort "parslets" by categories. Ex: Smiles, BBCodes, Formatters, etc.

*/

class Apriori
{
    function
Parse($Data, $Mode)
    {
        global
$PDO;
       
$Parsed = $Data;
       
$Parslets = mysql_query("SELECT * FROM Parslets WHERE Mode='".$Mode."'");

        while (
$Parslet = mysql_fetch_assoc($Parslets))
        {
                switch (
$Parslet["Type"])
                {
                    case
'Java': $Options = "jim"; break;
                    case
'GNU': $Options = "uim"; break;
                    case
'GREP': $Options = "gim"; break;
                    case
'EMACS': $Options = "cim"; break;
                    case
'Ruby': $Options = "rim"; break;
                    case
'Perl': $Options = "zim"; break;
                    case
'Posix': $Options = "bim"; break;
                    case
'PosixEx': $Options = "dim"; break;
                    default:
$Options = "dim"; break;
                }

               
mb_regex_set_options($Options);

               
$Output=null; // Variable for Parslet output.
               
$Inputs=array(); // Pockets for eregi. RTFM.
               
if (eregi($Parslet["Matcher"],$Parsed,$Inputs)) eval($Parslet["Replacer"]);
               
// Dynamic, or not - code must be evaluate once time

                // Main cycle. For every parslet, for every match.
               
while (eregi($Parslet["Matcher"],$Parsed,$Inputs))
                {
                    if (
$Parslet["isDynamic"]=="True") // Dynamic?
                   
{
                       
$Output=null; // Clean output
                       
eval($Parslet["Replacer"]); // Eval Replacer. Replacer's code must set "Output" variable.
                   
};
                   
$Parsed = str_replace($Inputs[0],$Output,$Parsed); // Replace and enjoy!
               
};
                
$Output=null;
        };
        return
$Parsed; // Yeah!
   
}
};

?>