A simple tag parsing library for PHP
Version 1.2
Simpletags is exactly what it sounds like...a simple way to use tags in your PHP application. This allows you to have tags that look like this:
{something:other}
{date format="m/d/Y"}
{blog:entries count="5"}
Stuff here
{/blog:entries}
You can send a config array to the constructor with the following options (these are the defaults):
array(
'l_delim' => '{',
'r_delim' => '{',
'trigger' => '',
);
You can also set the delimiters and triggers via the following functions:
$simpletags = new Simpletags();
$simpletags->set_delimitiers('{', '}');
$simpletags->set_trigger('foo:');
To parse a string of text you simply call the parse() function. The parse function accepts 3 parameters:
If no callback is specified then the function will return an array. Consider this is the content you sent:
Hello there.
{rest:get url="http://example.com/api" type="json"}
Stuff here
{/rest:get}
Bye.
Parse would return this:
Array
(
[content] => Hello there.
marker_0k0dj3j4nJHDj22j
Bye.
[tags] => Array
(
[0] => Array
(
[full_tag] => {rest:get url="http://example.com/api" type="json"}
Stuff here
{/rest:get}
[attributes] => Array
(
[url] => http://example.com/api
[type] => json
)
[segments] => Array
(
[0] => rest
[1] => get
)
[content] =>
Stuff here
[marker] => marker_0k0dj3j4nJHDj22j
)
)
)
The data array is a keyed array who's contents will replace tags with the same name. Example:
{foo:bar}
Would be replaced with "Hello World" when the following data array is sent to the parse function:
$data['foo']['bar'] = "Hello World"
You can use tag pairs to loop through data as well:
{books}
{title} by {author}<br />
{/books}
$data = array(
'books' => array(
array(
'title' => 'PHP for Dummies',
'author' => 'John Doe'
),
array(
'title' => 'CodeIgniter for Dummies',
'author' => 'Jane Doe'
)
)
);
PHP for Dummies by John Doe
CodeIgniter for Dummies by Jane Doe
The callback must be in a form that is_callable() accepts (typically array(object, method)). The callbac function should take 1 parameter (an array).
The callback will be sent the tag information in the form of an array. Consider the following
{rest:get url="http://example.com/api" type="json"}
Stuff here
{/rest:get}
Would send the callback function the following array:
Array
(
[full_tag] => {rest:get url="http://example.com/api" type="json"}
Stuff here
{/rest:get}
[attributes] => Array
(
[url] => http://example.com/api
[type] => json
)
[segments] => Array
(
[0] => rest
[1] => get
)
[content] =>
Stuff here
[marker] => marker_0k0dj3j4nJHDj22j
)
You use it the same as above, except you would use the following:
$this->load->library('simpletags');
$content = $this->simpletags->parse($content, $data, array($this, 'parser_callback'));