| Tutorial Ref.com | How to use the Text Highlighter with the PHP PEAR repository & CSS |
Tutorial Ref >> PHP Tutorials >> Text Highlighter PHP PEAR bug
Date modified - May 5, 2009
1) First you need to install the PEAR TEXT_HIGHLIGHTER here:PEAR PHP Text_Highlighter
Type this: pear install Text_Highlighter-0.7.1 (or whatever is the latest, stable version and replace with the appropriate numbers). I had a problem with Bluehost.com where I currently host some of my websites. I ran this pear install Text_Highlighter-0.7.1 via PuTTY;however, I received an error that the Text Highlighter files could not be found. So I attempted to change the php.ini file on the line include_path but it didn't work. So I used the PHP set_include_path() function as so:
1 2 3 4 | $path = "{path/to/PEAR}"; $set = set_include_path(get_include_path() . PATH_SEPARATOR . $path); |
2) Then I set a require_once() function to the Text Highlighter file. I placed the following code right after my body tags:
1 2 3 4 5 | require_once "Text/Highlighter.php"; require_once "Text/Highlighter/Renderer/Html.php"; $renderer = new Text_Highlighter_Renderer_Html(array("numbers" => HL_NUMBERS_LI, "tabsize" => 4)); |
The $render object set by Text_Highlighter_Renderer_Html() sets numbers for each line of code and the othe r option sets the tabs for easier reading. I haven't quite figured out how the tab works though. When I figure this out I'll post the solution. Right after the above code I set all the variables for each specific programming language as so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | $hiHtml =& Text_Highlighter::factory("HTML"); $hiHtml->setRenderer($renderer); $hiCss =& Text_Highlighter::factory("CSS"); $hiCss->setRenderer($renderer); $hiPhp =& Text_Highlighter::factory("PHP"); $hiPhp->setRenderer($renderer); $hiDtd =& Text_Highlighter::factory("DTD"); $hiDtd->setRenderer($renderer); $hiJava =& Text_Highlighter::factory("Java"); $hiJava->setRenderer($renderer); $hiJavascript =& Text_Highlighter::factory("Javascript"); $hiJavascript->setRenderer($renderer); $hiMySql =& Text_Highlighter::factory("MySQL"); $hiMySql->setRenderer($renderer); $hiPerl =& Text_Highlighter::factory("Perl"); $hiPerl->setRenderer($renderer); $hiPython =& Text_Highlighter::factory("Python"); $hiPython->setRenderer($renderer); $hiRuby =& Text_Highlighter::factory("Ruby"); $hiRuby->setRenderer($renderer); $hiSql =& Text_Highlighter::factory("SQL"); $hiSql->setRenderer($renderer); $hiVbScript =& Text_Highlighter::factory("VBSCRIPT"); $hiVbScript->setRenderer($renderer); $hiXml =& Text_Highlighter::factory("XML"); $hiXml->setRenderer($renderer); |
To implement the highlighting in your code all you need to do is:
1 2 3 4 5 | echo $hiHtml->highlight("<p>This is a sample of displaying and preformatting HTML code to display properly and that validates under W3C standards.</p> <div>This is a div tag. I've used the character entities for the HTML P tage and in the DIV tag</div> <div>In this div tag I've used the numbered entities for the less than and greater than signs. Though you can't see it displayed in the browser you can check in the source code of the browser.</div>") |
I found the only way to highlight the code is the add the CSS code found from a link on Sitepoint-PHP Text Highlighter. From there I found a link to a CSS file for PEAR's Text Highlighter. Include this CSS file in your <head> section like so:
1 | <link rel="stylesheet" href="./styles/hilight.css" media="screen" type="text/css" /> |
I found a slight differenc when using single quotes for the highlight function and double-quotes. I use the single quotes immediately within the highlight function and double-quotes for the rest of the characters such as for attributes. Notice the first highlight:
1 | <span style="font:15px;">Notice the single quotes just within the brackets. As well, notice the double-quotes within the style attribute.</span> |
1 | <span style='font:15px;'>Notice the single quotes just within the brackets. As well, notice the double-quotes within the style attribute.</span> |
I hope this helps someone. Happy coding!