Introduction | Zend_Markup Programmer's Reference Guide | Zend_Markup Parsers

# Getting Started With Zend_Markup

This guide to get you started with Zend_Markup uses the BBCode parser and HTML renderer. The priciples discussed can be adapted to other parsers and renderers.

Example #1 Basic Zend_Markup Usage

We will first instantiate a Zend_Markup_Renderer_Html object using the Zend_Markup::factory() method. This will also create a Zend_Markup_Parser_Bbcode object which will be added to the renderer object.

Afther that, we will use the render() method to convert a piece of BBCode to HTML.

1. // Creates instance of Zend_Markup_Renderer_Html,
2. // with Zend_Markup_Parser_BbCode as its parser
3. $bbcode = Zend_Markup::factory('Bbcode');
5. echo $bbcode->render('[b]bold text[/b] and [i]cursive text[/i]');
6. // Outputs: '<strong>bold text</strong> and <em>cursive text</em>'

Example #2 A more complicated example of Zend_Markup

This time, we will do exactly the same as above, but with more complicated BBCode markup.

1. $bbcode = Zend_Markup::factory('Bbcode');
3. $input = <<<EOT
4. [list]
5. [*]Zend Framework
6. [*]Foobar
7. [/list]
8. EOT;
10. echo $bbcode->render($input);
11. /*
12. Should output something like:
13. <ul>
14. <li>Zend Framework</li>
15. <li>Foobar</li>
16. </ul>
17. */

Example #3 Processing incorrect input

Besides simply parsing and rendering markup such as BBCode, Zend_Markup is also able to handle incorrect input. Most BBCode processors are not able to render all input to XHTML valid output. Zend_Markup corrects input that is nested incorrectly, and also closes tags that were not closed:

1. $bbcode = Zend_Markup::factory('Bbcode');
3. echo $bbcode->render('some [i]wrong [b]sample [/i] text');
4. // Note that the '[b]' tag is never closed, and is also incorrectly
5. // nested; regardless, Zend_Markup renders it correctly as:
6. // some <em>wrong <strong>sample </strong></em><strong> text</strong>

Introduction | Zend_Markup Programmer's Reference Guide | Zend_Markup Parsers