Skip to content

Instantly share code, notes, and snippets.

@nerdsrescueme
Created October 27, 2011 20:33
Show Gist options
  • Save nerdsrescueme/1320783 to your computer and use it in GitHub Desktop.
Save nerdsrescueme/1320783 to your computer and use it in GitHub Desktop.
Tokenizer
<?php
class Tokenizer {
protected $tokenFormat = '[! %s !]'
protected $rules = array();
protected $raw;
public function execute($input)
{
$this->raw = $input;
foreach($this->rules as $token => $rule)
{
if($this->isClosable($token))
{
$search = array($rule['open'], $rule['close']);
$replace = array(
sprintf($this->tokenFormat, 'OPEN_'.$token),
sprintf($this->tokenFormat, 'CLOSE_'.$token),
);
$input = preg_replace($search, $replace, $input);
}
}
}
protected function isClosable($token)
{
return isset($this->rules[$token]['close'];
}
protected function isNestable($key)
{
return isset($this->rules[$token]['nested']) and $this->rules[$key]['nested'];
}
}
class Markdown {
protected $rules = array(
'EM' => array('open' => '\s_\S|\s\*\S'), 'close' => '\S_\s|\S\*\s'),
'STRONG' => array('open' => '\s__\S|\s\*\*\S'), 'close' => '\S__\s|\S\*\*\s'),
'EM_STRONG' =>array('open' => '\s___\S|\s\*\*\*\S'), 'close' => '\S___\s|\S\*\*\*\s'),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment