Re: How to write a simple parser?

From: Date: Tue, 28 Nov 2000 01:04:59 +0000
Subject: Re: How to write a simple parser?
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-27530@lists.php.net to get a copy of this message
I have a small class for this - it's attached for your viewing pleasure. --Joe On Mon, Nov 27, 2000 at 06:26:34PM -0600, Jason wrote: > On Mon, Nov 27, 2000 at 07:06:44PM -0800, rodrigo wrote: > > I neeed to write a simple parser that will interpret simple commands and > > convert them to HTML. The command tags start and end with square > > brackets "[]". For example, if someone where to type > > "[link:www.locatio.com | locatio]" then that would translate into "<a > > href="www.locatio.com">locatio</a>". > > If you plan on growing your parser to anything significant, you might want > to take a look at PHP's XML support. XML is really useful for config file > type stuff, and is becoming more and more widespread. > > But if all you want to do is your one example, it'd be overkill. > > read all about it at http://www.php.net/manual/ref.xml.php > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net ================================================================================ = Joe Stump joestump98@yahoo.com http://www.miester.org = ================================================================================ Need a programmer? http://www.google.com/search?q=joe+stump+resume

<? /********************************************************************************\ * Copyright (C) Joe Stump <joestump98@yahoo.com>, * * Emir Musabasic <emusabasic@softhome.net> * * * * This program is free software; you can redistribute it and/or * # modify it under the terms of the GNU General Public License * # as published by the Free Software Foundation; either version 2 * # of the License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * \********************************************************************************/ // Created By: Joe Stump // Created On: 2000-11-13 // History: // 2000-11-13 : Created // // Used to parse custom HTML tags. It also highlights // PHP code between [pre] and [/pre] tags on the fly. // // Supported Tags: // - [b] this will bold text [/b] // - [i] this will make text italics [/i] // - [p] returns <p> // - \r is nullified // - \n returns <br> // - \n\n returns <p> // - [pre]<? $var = foo; ?>[/pre] will highlight the code // - [url]http://www.miester.org[/url] returns <a href="http://www.miester.org">http://www.miester.org</a> // - [url=http://www.miester.org]miester[/url] returns <a href="http://www.miester.org">miester</a> // - [indent] and [/indent] return <blockquote> and </blockquote> class HTMLparser{ var $input; var $highlighted_html; var $parsed_html; function set_input($new_input){ $this->input = $new_input; } function return_input(){ return $this->input; } function set_parsed_html($new_parsed_html){ $this->parsed_html = $new_parsed_html; } function return_parsed_html(){ return $this->parsed_html; } function set_highlighted_html($new_highlighted_html){ $this->highlighted_html = $new_highlighted_html; } function return_highlighted_html(){ return $this->highlighted_html; } function HTMLparser($input){ $this->set_input($input); $this->HighlightHTML(); $this->ParseHTML(); } // Copyleft Emir Musabasic <emusabasic@softhome.net> function ParseHTML(){ $output = eregi_replace("\[[Bb]]","<b>",$this->retrun_highlighted_html()); $output = eregi_replace("\[/[Bb]]","</b>",$output); $output = eregi_replace("\[[Ii]]","<i>",$output); $output = eregi_replace("\[/[Ii]]","</i>",$output); $output = eregi_replace("\[[Pp]]","<p>",$output); $output = ereg_replace("\r","",$output); $output = ereg_replace("\n\n","<p>",$output); $output = ereg_replace("\n","<br>",$output); $output = eregi_replace("\[[Pp][Rr][Ee]]","\n<pre>\n",$output); $output = eregi_replace("\[/[Pp][Rr][Ee]]","\n</pre>\n",$output); $output = eregi_replace("\[[Ii][Nn][Dd][Ee][Nn][Tt]]","<blockquote>",$output); $output = eregi_replace("\[/[Ii][Nn][Dd][Ee][Nn][Tt]]","</blockquote>",$output); $output = ereg_replace("\[[Uu][Rr][Ll]]([-_./a-zA-Z0-9!&%#?,'=:~]+)\[/[Uu][Rr][Ll]]", "<A HREF=\"\\1\">\\1</A>", $output); $output = ereg_replace("\[[Uu][Rr][Ll]=([-_./a-zA-Z0-9!&%#?,'=:~]+)]". "([-_./a-zA-Z0-9 !&%#?,'=:~]+)\[/[Uu][Rr][Ll]]", "<A HREF=\"\\1\">\\2</A>", $output); $this->set_parsed_html($output); } function HighlightPHP(){ ob_start(); highlight_string(stripslashes($this->input)); $this->set_highlighted_html(str_replace('&nbsp;',' ',ob_get_contents())); ob_end_clean(); } } ?>
« previous php.general (#27530) next »