Archive for July 30th, 2010
Text Highlight PHP:TextHighlighter PEAR CSS File
Recently I had a problem with the Text Highlighter obtained from the PHP PEAR repository. I thought I was following all the instructions but it seemed the code was not highlighting the text with color i.e. beautifying it. Here are the steps I took to get the Text_Highlighter working on this site:
1 First you need to install the PEAR TEXT_HIGHLIGHTER. Google ‘pear php.’ Then search for ‘text highlighter.’
Type this: pear install Text_Highlighter0.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_Highlighter0.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:
lt;?php
path = “path/to/PEAR”;
set = set_include_pathget_include_path . PATH_SEPARATOR . path;
?gt;
2 Then I set a require_once function to the Text Highlighter file. I placed the following code right after my body tags:
lt;?php
require_once “Text/Highlighter.php”;
require_once “Text/Highlighter/Renderer/Html.php”;
renderer = new Text_Highlighter_Renderer_Htmlarray”numbers” =gt; HL_NUMBERS_LI “tabsize” =gt; 4;
?gt;
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:
lt;?php
hiHtml =amp; Text_Highlighter::factory”HTML”;
hiHtmlgt;setRendererrenderer;
hiCss =amp; Text_Highlighter::factory”CSS”;
hiCssgt;setRendererrenderer;
hiPhp =amp; Text_Highlighter::factory”PHP”;
hiPhpgt;setRendererrenderer;
hiDtd =amp; Text_Highlighter::factory”DTD”;
hiDtdgt;setRendererrenderer;
hiJava =amp; Text_Highlighter::factory”Java”;
hiJavagt;setRendererrenderer;
hiJavascript =amp; Text_Highlighter::factory”Javascript”;
hiJavascriptgt;setRendererrenderer;
hiMySql =amp; Text_Highlighter::factory”MySQL”;
hiMySqlgt;setRendererrenderer;
hiPerl =amp; Text_Highlighter::factory”Perl”;
hiPerlgt;setRendererrenderer;
hiPython =amp; Text_Highlighter::factory”Python”;
hiPythongt;setRendererrenderer;
hiRuby =amp; Text_Highlighter::factory”Ruby”;
hiRubygt;setRendererrenderer;
hiSql =amp; Text_Highlighter::factory”SQL”;
hiSqlgt;setRendererrenderer;
hiVbScript =amp; Text_Highlighter::factory”VBSCRIPT”;
hiVbScriptgt;setRendererrenderer;
hiXml =amp; Text_Highlighter::factory”XML”;
hiXmlgt;setRendererrenderer;
?gt;
To implement the highlighting in your code all you need to do is:
lt;?php
echo hiHtmlgt;highlight”lt;pgt;This is a sample of displaying and preformatting HTML code to display properly and that validates under W3C standards.lt;/pgt;
lt;divgt;This is a div tag. I’ve used the character entities for the HTML P tage and in the DIV taglt;/divgt;
lt;divgt;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.lt;/divgt;”
?gt;
About the writer: TutorialRef.com is a website hosting articles on various tutorials on PHP CSS Javascript AJAX XML other computer languages SEO Search Engine Optimization Internet Marketing software tips and other subject matters. This article is written by Victor Kimura a web developer and SEO expert. PHP Tutorial. Original article for stepbystep PHP Tutorial with highlighted code: Text Highlighter PHP PEAR.
