Ik heb voor mijn volgende project een class gemaakt die regels kan matchen. Als hij een regel heeft gematcht dan voert hij daarbij de bijbehorende functie uit.
Voorbeeld met uitleg:
De class:
Voorbeeld met uitleg:
[COLOR= #0000BB]<?php[/COLOR]
[URL="http://www.php.net/require_once"]require_once[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]class.php[/COLOR][COLOR= #007700]);[/COLOR]
[COLOR= #FF8000]//Maak de class aan.[/COLOR]
[COLOR= #0000BB]$recognizer[/COLOR] [COLOR= #007700]= new[/COLOR] Text_Recognizer[COLOR= #007700]();[/COLOR]
[COLOR= #FF8000]//Creeer een callback function (Kan je zelf ook in een class doen)[/COLOR]
[COLOR= #007700]function[/COLOR] create_user[COLOR= #007700]([/COLOR][COLOR= #0000BB]$params[/COLOR][COLOR= #007700])
{[/COLOR]
[COLOR= #FF8000]//Gebruiker aanmaken
//De return is de return van de recognize functie. (Zie later in het script)[/COLOR]
[URL="http://www.php.net/return"]return[/URL] [COLOR= #DD0000]Gebruiker aangemaakt (Gebruikersnaam: [/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$params[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]username[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #DD0000], wachtwoord: [/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$params[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]password[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #DD0000])[/COLOR][COLOR= #007700];
}[/COLOR]
[COLOR= #FF8000]//Maak een shortcut aan voor veelkomende combinaties. (In die geval het vinden van een gebruikersnaam)[/COLOR]
[COLOR= #0000BB]$recognizer[/COLOR][COLOR= #007700]->[/COLOR]addShortcut[COLOR= #007700](new[/COLOR] Text_Recognizer_Shortcut[COLOR= #007700]([/COLOR][COLOR= #DD0000]username[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #DD0000](?P<username>[A-Za-z][A-Za-z0-9_]{2,})[/COLOR][COLOR= #007700]));[/COLOR]
[COLOR= #FF8000]//Voeg een regel toe die de recognizer kan herkennen. Als de regel is gevonden dan roept hij in dit geval de functie "create_user" aan.
// De array in de 3de parameter zijn de standaard gedineerde parameters voor de callback functie.[/COLOR]
[COLOR= #0000BB]$recognizer[/COLOR][COLOR= #007700]->[/COLOR]addOption[COLOR= #007700](new[/COLOR] Text_Recognizer_Option[COLOR= #007700]([/COLOR][COLOR= #DD0000]Maak gebruiker aan met de naam {username}[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #DD0000]create_user[/COLOR][COLOR= #007700],[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]password[/COLOR] [COLOR= #007700]=>[/COLOR] [COLOR= #DD0000]wachtwoordje[/COLOR][COLOR= #007700])));[/COLOR]
[COLOR= #FF8000]//Zoek in alle opties en kijk of de regel herkent kan worden.[/COLOR]
[URL="http://www.php.net/print_r"]print_r[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$recognizer[/COLOR][COLOR= #007700]->[/COLOR]recognize[COLOR= #007700]([/COLOR][COLOR= #DD0000]Maak gebruiker aan met de naam jenner[/COLOR][COLOR= #007700]));[/COLOR]
[COLOR= #FF8000]// In de expression betekent "(?P<username>HIER JE VERDERE EXPRESSION)" dat dat deel van je expression wordt gedefineerd met username.
// Deze kun je makkelijk aanroepen met $params (zie callback functie)[/COLOR]
?> De class:
Spoiler tonen
[COLOR= #0000BB]<?php[/COLOR]
[URL="http://www.php.net/class"]class[/URL] Text_Recognizer [COLOR= #007700]{[/COLOR]
[COLOR= #FF8000]/**
* The array with all the options
*
* @type array
*/[/COLOR]
[URL="http://www.php.net/private"]private[/URL] [COLOR= #0000BB]$options[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]();[/COLOR]
[COLOR= #FF8000]/**
* The array with all the shortcuts
*
* @type array
*/[/COLOR]
[URL="http://www.php.net/private"]private[/URL] [COLOR= #0000BB]$shortcuts[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]();[/COLOR]
[COLOR= #FF8000]/**
* Always define a construct function
*/[/COLOR]
[URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] __construct[COLOR= #007700]() { }[/COLOR]
[COLOR= #FF8000]/**
* Call this function to recognize text
*
* @param string $text The text you hope we can recognize
*
* @return mixed The return of the callback otherwise the input text.
*/[/COLOR]
[URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] recognize[COLOR= #007700]([/COLOR][COLOR= #0000BB]$text[/COLOR][COLOR= #007700])
{[/COLOR]
[URL="http://www.php.net/if"]if[/URL][COLOR= #007700](![/COLOR][URL="http://www.php.net/is_string"]is_string[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$text[/COLOR][COLOR= #007700]))[/COLOR]
throw [COLOR= #007700]new[/COLOR] Exception[COLOR= #007700]([/COLOR][COLOR= #DD0000]The text you want to recognize isn a string.[/COLOR][COLOR= #007700]);[/COLOR]
[URL="http://www.php.net/foreach"]foreach[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]getOptions[COLOR= #007700]() as[/COLOR] [COLOR= #0000BB]$option[/COLOR][COLOR= #007700])
{[/COLOR]
[COLOR= #0000BB]$expression[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]/A[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]_replace_shortcuts[COLOR= #007700]([/COLOR][COLOR= #0000BB]$option[/COLOR][COLOR= #007700]->[/COLOR]getExpression[COLOR= #007700]()).[/COLOR][COLOR= #DD0000]/[/COLOR][COLOR= #007700];[/COLOR]
[URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/preg_match"]preg_match[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$expression[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$text[/COLOR][COLOR= #007700]))
{[/COLOR]
[URL="http://www.php.net/preg_match"]preg_match[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$expression[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$text[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$matches[/COLOR][COLOR= #007700]);[/COLOR]
[COLOR= #0000BB]$params[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array_merge"]array_merge[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$option[/COLOR][COLOR= #007700]->[/COLOR]getDefaults[COLOR= #007700](),[/COLOR] [COLOR= #0000BB]$matches[/COLOR][COLOR= #007700]);[/COLOR]
[COLOR= #0000BB]$result[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/call_user_func"]call_user_func[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$option[/COLOR][COLOR= #007700]->[/COLOR]getCallback[COLOR= #007700](),[/COLOR] [COLOR= #0000BB]$params[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$option[/COLOR][COLOR= #007700]);[/COLOR]
[URL="http://www.php.net/return"]return[/URL] [COLOR= #0000BB]$result[/COLOR][COLOR= #007700];
}
}[/COLOR]
throw [COLOR= #007700]new[/COLOR] Exception[COLOR= #007700]([/COLOR][COLOR= #DD0000]We couldn match the text.[/COLOR][COLOR= #007700]);
}[/COLOR]
[COLOR= #FF8000]/**
* Add a option for the recognize function.
*
* @param Text_Recognizer_Option $option The class with all the option info.
*
* @return Text_Recognizer_Option The option
*/[/COLOR]
[URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] addOption[COLOR= #007700]([/COLOR]Text_Recognizer_Option [COLOR= #0000BB]$option[/COLOR][COLOR= #007700])
{[/COLOR]
[COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]options[COLOR= #007700][] =[/COLOR] [COLOR= #0000BB]$option[/COLOR][COLOR= #007700];[/COLOR]
[URL="http://www.php.net/return"]return[/URL] [COLOR= #0000BB]$option[/COLOR][COLOR= #007700];
}[/COLOR]
[COLOR= #FF8000]/**
* Return all the options
*
* @return array All the options
*/[/COLOR]
[URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] getOptions[COLOR= #007700]()
{[/COLOR]
[URL="http://www.php.net/return"]return[/URL] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]options[COLOR= #007700];
}[/COLOR]
[COLOR= #FF8000]/**
* Add a shortcut for expressions inside the option expression
*
* @param Text_Recognizer_Shortcut $shortcut The class with all the info about the shortcut.
*
* @return Text_Recognizer_Shortcut The shortcut
*/[/COLOR]
[URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] addShortcut[COLOR= #007700]([/COLOR]Text_Recognizer_Shortcut [COLOR= #0000BB]$shortcut[/COLOR][COLOR= #007700])
{[/COLOR]
[COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]shortcuts[COLOR= #007700][] =[/COLOR] [COLOR= #0000BB]$shortcut[/COLOR][COLOR= #007700];[/COLOR]
[URL="http://www.php.net/return"]return[/URL] [COLOR= #0000BB]$shortcut[/COLOR][COLOR= #007700];
}[/COLOR]
[COLOR= #FF8000]/**
* Return all the shortcuts
*
* @return array All the shortcuts in an array.
*/[/COLOR]
[URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] getShortcuts[COLOR= #007700]()
{[/COLOR]
[URL="http://www.php.net/return"]return[/URL] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]shortcuts[COLOR= #007700];
}[/COLOR]
[COLOR= #FF8000]/**
* Replace in the options all the shortcuts
*
* @param string $expression A expression with shortcuts
*
* @return string The replaced expression
*/[/COLOR]
[URL="http://www.php.net/private"]private[/URL] [COLOR= #007700]function[/COLOR] _replace_shortcuts[COLOR= #007700]([/COLOR][COLOR= #0000BB]$expression[/COLOR][COLOR= #007700])
{[/COLOR]
[COLOR= #0000BB]$search[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]();[/COLOR]
[COLOR= #0000BB]$replace[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]();[/COLOR]
[URL="http://www.php.net/foreach"]foreach[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]getShortcuts[COLOR= #007700]() as[/COLOR] [COLOR= #0000BB]$shortcut[/COLOR][COLOR= #007700])
{[/COLOR]
[COLOR= #0000BB]$search[/COLOR][COLOR= #007700][] =[/COLOR] [COLOR= #DD0000]{[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$shortcut[/COLOR][COLOR= #007700]->[/COLOR]getTitle[COLOR= #007700]().[/COLOR][COLOR= #DD0000]}[/COLOR][COLOR= #007700];[/COLOR]
[COLOR= #0000BB]$replace[/COLOR][COLOR= #007700][] =[/COLOR] [COLOR= #0000BB]$shortcut[/COLOR][COLOR= #007700]->[/COLOR]getExpression[COLOR= #007700]();
}[/COLOR]
[COLOR= #0000BB]$replaced[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/str_replace"]str_replace[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$search[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$replace[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$expression[/COLOR][COLOR= #007700]);[/COLOR]
[URL="http://www.php.net/return"]return[/URL] [COLOR= #0000BB]$replaced[/COLOR][COLOR= #007700];
}
}[/COLOR]
[URL="http://www.php.net/class"]class[/URL] Text_Recognizer_Option [COLOR= #007700]{[/COLOR]
[COLOR= #FF8000]/**
* The expression to match the option
*
* @type string
*/[/COLOR]
[URL="http://www.php.net/private"]private[/URL] [COLOR= #0000BB]$expression[/COLOR][COLOR= #007700];[/COLOR]
[COLOR= #FF8000]/**
* The callback for calling when there is a match
*
* @type callback
*/[/COLOR]
[URL="http://www.php.net/private"]private[/URL] [COLOR= #0000BB]$callback[/COLOR][COLOR= #007700];[/COLOR]
[COLOR= #FF8000]/**
* The defaults for the settings in the callback
*
* @type array
*/[/COLOR]
[URL="http://www.php.net/private"]private[/URL] [COLOR= #0000BB]$defaults[/COLOR][COLOR= #007700];[/COLOR]
[COLOR= #FF8000]/**
* Construct a option class
*
* @param string $expression A expression for recognizing.
* @param callback $callback
*/[/COLOR]
[URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] __construct[COLOR= #007700]([/COLOR][COLOR= #0000BB]$expression[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$callback[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$defaults[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]())
{[/COLOR]
[URL="http://www.php.net/if"]if[/URL][COLOR= #007700](![/COLOR][URL="http://www.php.net/is_string"]is_string[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$expression[/COLOR][COLOR= #007700]))[/COLOR]
throw [COLOR= #007700]new[/COLOR] Exception[COLOR= #007700]([/COLOR][COLOR= #DD0000]The expression in your option isn a correct expression[/COLOR][COLOR= #007700]);[/COLOR]
[URL="http://www.php.net/if"]if[/URL][COLOR= #007700](![/COLOR][URL="http://www.php.net/is_callable"]is_callable[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$callback[/COLOR][COLOR= #007700]))[/COLOR]
throw [COLOR= #007700]new[/COLOR] Exception[COLOR= #007700]([/COLOR][COLOR= #DD0000]The callback in your option isn callable.[/COLOR][COLOR= #007700]);[/COLOR]
[URL="http://www.php.net/if"]if[/URL][COLOR= #007700](![/COLOR][URL="http://www.php.net/is_array"]is_array[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$defaults[/COLOR][COLOR= #007700]))[/COLOR]
throw [COLOR= #007700]new[/COLOR] Exception[COLOR= #007700]([/COLOR][COLOR= #DD0000]The defaults in your option aren a array.[/COLOR][COLOR= #007700]);[/COLOR]
[COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]expression [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$expression[/COLOR][COLOR= #007700];[/COLOR]
[COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]callback [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$callback[/COLOR][COLOR= #007700];[/COLOR]
[COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]defaults [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$defaults[/COLOR][COLOR= #007700];
}[/COLOR]
[COLOR= #FF8000]/**
* Return the expression
*
* @return string Expression
*/[/COLOR]
[URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] getExpression[COLOR= #007700]()
{[/COLOR]
[URL="http://www.php.net/return"]return[/URL] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]expression[COLOR= #007700];
}[/COLOR]
[COLOR= #FF8000]/**
* Return the callback
*
* @return callback Callback
*/[/COLOR]
[URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] getCallback[COLOR= #007700]()
{[/COLOR]
[URL="http://www.php.net/return"]return[/URL] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]callback[COLOR= #007700];
}[/COLOR]
[COLOR= #FF8000]/**
* Return the defaults
*
* @return array The defaults
*/[/COLOR]
[URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] getDefaults[COLOR= #007700]()
{[/COLOR]
[URL="http://www.php.net/return"]return[/URL] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]defaults[COLOR= #007700];
}
}[/COLOR]
[URL="http://www.php.net/class"]class[/URL] Text_Recognizer_Shortcut [COLOR= #007700]{[/COLOR]
[COLOR= #FF8000]/**
* The title for the shortcut
*
* @type string Title
*/[/COLOR]
[URL="http://www.php.net/private"]private[/URL] [COLOR= #0000BB]$title[/COLOR][COLOR= #007700];[/COLOR]
[COLOR= #FF8000]/**
* The expression to replace the shortcut
*
* @type string Expression
*/[/COLOR]
[URL="http://www.php.net/private"]private[/URL] [COLOR= #0000BB]$expression[/COLOR][COLOR= #007700];[/COLOR]
[URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] __construct[COLOR= #007700]([/COLOR][COLOR= #0000BB]$title[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$expression[/COLOR][COLOR= #007700])
{[/COLOR]
[URL="http://www.php.net/if"]if[/URL][COLOR= #007700](![/COLOR][URL="http://www.php.net/is_string"]is_string[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$title[/COLOR][COLOR= #007700]) || ![/COLOR][URL="http://www.php.net/preg_match"]preg_match[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]/[A-Za-z0-9_]+/[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$title[/COLOR][COLOR= #007700]))[/COLOR]
throw [COLOR= #007700]new[/COLOR] Exception[COLOR= #007700]([/COLOR][COLOR= #DD0000]The title in your shortcut isn a correct title. (Only: [A-Za-z0-9_])[/COLOR][COLOR= #007700]);[/COLOR]
[URL="http://www.php.net/if"]if[/URL][COLOR= #007700](![/COLOR][URL="http://www.php.net/is_string"]is_string[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$expression[/COLOR][COLOR= #007700]))[/COLOR]
throw [COLOR= #007700]new[/COLOR] Exception[COLOR= #007700]([/COLOR][COLOR= #DD0000]The expression in your shortcut isn a correct expression[/COLOR][COLOR= #007700]);[/COLOR]
[COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]title [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$title[/COLOR][COLOR= #007700];[/COLOR]
[COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]expression [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$expression[/COLOR][COLOR= #007700];
}[/COLOR]
[COLOR= #FF8000]/**
* Return the title of the shortcut
*
* @return string The title
*/[/COLOR]
[URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] getTitle[COLOR= #007700]()
{[/COLOR]
[URL="http://www.php.net/return"]return[/URL] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]title[COLOR= #007700];
}[/COLOR]
[COLOR= #FF8000]/**
* Return the expression
*
* @return string Expression
*/[/COLOR]
[URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] getExpression[COLOR= #007700]()
{[/COLOR]
[URL="http://www.php.net/return"]return[/URL] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]expression[COLOR= #007700];
}
}[/COLOR]
?>