Project Description
A PHP plugin for fast parsing of Regex Tokens into different files on request and the ability to register different handlers for each type of token.

Magic Token PHP


Download and Unzip the latest release into your directory of choice. The magic_token.php file acts as the initializer and config file; open it and modify the $plugin_relative_location to the location that fits your current setup. You will also include any files in the $magic_token_required_files that you wish to automatically load and/or use as a service with AJAX.

Basic Usage

This introduction is not to teach you how to use PHP but rather understand how the Magic Token PHP plugin works.

The following is the basic usage for running a simple token parse on a string. The Magic Token PHP plugin uses standard PHP Regex. The initial '/' delimiter is automatically added and appended to the token you supply.


$text = "Hello my name is [@test]";

// The parse master class handles the processing of the tokens
$parser = new magic_token\parsers\parse_master();

// Register the class named "debug_consumer" to handle the token if found. Make sure to include any namespaces as well
$parser->registerToken("\[\@([A-Za-z]+)\]", "magic_token\consumers\debug_consumer");

$returned_string = $parser->parse($text);


The debug_consumer class is included in the source code and will help you debug the output of a certain token. The following is the sample output from the above code:

*Magic Token Debug Consumer*
Found the following matches for this token: */\[\@([A-Za-z]+)\]/*
[0] - [@test]
[1] - test

Understanding the Consumer

The token_consumer class is the base class that you will extend to handle your tokens. The class should have a function named "consume" with the following function signature:

class my_consumer extends magic_token\consumers\token_consumer
	 * Consumes the token and innerText passed to it.
	 * @param $stringRef The reference to the original string that is being parsed.
	 * @param $token The original token that use found.
	 * @param $match The raw match string that was found
	 * @param $groups The groups of the regex
	public function consume(&$stringRef, $token, $match, $groups = array())
                // .. your code goes here .. //
		return true;

You can modify the return string simply by making changes to the stringRef variable in the function parameters. You must return true in order for the parse_master class to continue to loop through all available consumers.

Register Your Consumer

Next you must register your consumer with the parser.

$token = "\[\@([A-Za-z]+)\]"; // ie: [@var_name]
$parser = new magic_token\parsers\parse_master();
$parser->registerToken($token, "my_consumer");

In the above code, you are registering the "my_consumer" class to handle the processing of that specific token. You can register as many handlers as you would like to the parser class.

Parse a File

Simply use the function below instead to parse a file at the include path location of $file.
$result = $parser->parseFile("../somewhere/dynamic.css");
echo $result;

AJAX Parsing

The current version has a supplied service on the magic_token.php file. Simply pass a get request in the following format: consumer)&token=(your token)&string=(string to parse)

The parser will echo the output back after it has been parsed.

Last edited Jan 4, 2013 at 8:15 PM by dklompmaker, version 17