Miscellaneous Code

  Home arrow Miscellaneous Code arrow Custom tag parser
MISCELLANEOUS CODE

Custom tag parser
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2003-05-05

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement
    this class is used for custom tag parsing in some text or data you might have. Parses the tags and cals the function for it ..

    also supports attributes

    use ..
    $text="
    data
    data
    data
    ";



    By : armandoxxx

    <?php

    /**
    * My Tag class
    * @author Armando Ota <armando.ota@rs-pi.com>
    * @version 1.1
    * @since 28.04.2003, 08:33:49
    * @copyright GPL - jada jada jada
    **/


    define("MYTAG_PREFIX",""); //prefixes you may use
    define("MYTAG_CONVERT_TO_BR","1"); //converts CR to HTML <BR />
    define("MYTAG_SEPARATOR",""); //defines separator



    class myTag {

    var $tag_stack = null;
    var $basic_string = "";

    var $CONVERT_TO_BR = 0;
    /**
    * constructor
    * @return
    * @version 1.1
    * @author Armando Ota <armando.ota@rs-pi.com>
    * @since 28.04.2003, 08:43:02
    **/
    function myTag(){
    $this->CONVERT_TO_BR=MYTAG_CONVERT_TO_BR;
    }


    /**
    * definition functions
    * INPUT PARAMS ARE NECESSERY !!!
    * @param Array $attr=null array of attributes you can use with tag
    * @param String $data="" data you want do parse
    * @return String
    * @version 1.1
    * @author Armando Ota <armando.ota@rs-pi.com>
    * @since 28.04.2003, 16:37:34
    **/

    /**
    * external link
    * @param array $attr=null attributes array
    * @param string $data="" input data
    * @return string
    * @version 1.0
    * @author Armando Ota <armando.ota@rs-pi.com>
    * @since 05.05.2003, 08:09:06
    **/
    function link($attr=null,$data=""){
    if($attr['url']){
    $output="<a href=\"".$attr['url']."\" target=\"_blank\">".$data."</a>";
    } else {
    $output=$data;
    }
    return $output;
    }

    /**
    * internal link
    * @param array $attr=null attributes array
    * @param string $data="" input data
    * @return string
    * @version 1.0
    * @author Armando Ota <armando.ota@rs-pi.com>
    * @since 05.05.2003, 08:09:06
    **/
    function int_link($attr=null,$data=""){
    return $output;
    }

    /**
    * file into content
    * @param array $attr=null attributes array
    * @param string $data="" input data
    * @return string
    * @version 1.0
    * @author Armando Ota <armando.ota@rs-pi.com>
    * @since 05.05.2003, 08:09:06
    **/
    function file($attr=null,$data=""){
    return $output;
    }


    /**
    * shows picture in content
    * @param array $attr=null attributes array
    * @param string $data="" input data
    * @return string
    * @version 1.0
    * @author Armando Ota <armando.ota@rs-pi.com>
    * @since 05.05.2003, 08:09:06
    **/
    function picture($attr=null,$data=""){
    return $output;
    }


    /**
    * creates table in content
    * @param array $attr=null attributes array
    * @param string $data="" input data
    * @return string
    * @version 1.0
    * @author Armando Ota <armando.ota@rs-pi.com>
    * @since 05.05.2003, 08:09:06
    **/
    function table($attr=null,$data=""){
    $output="
    <table cellspacing=\"0\" cellpadding=\"0\" border=\"0\">
    ".$data."
    </table>
    ";
    return $output;
    }

    /**
    * creates table row in content
    * @param array $attr=null attributes array
    * @param string $data="" input data
    * @return string
    * @version 1.0
    * @author Armando Ota <armando.ota@rs-pi.com>
    * @since 05.05.2003, 08:09:06
    **/
    function row($attr=null,$data=""){
    $output="
    <tr>
    ".$data."
    </tr>
    ";
    return $output;
    }

    /**
    * creates table column in content
    * @param array $attr=null attributes array
    * @param string $data="" input data
    * @return string
    * @version 1.0
    * @author Armando Ota <armando.ota@rs-pi.com>
    * @since 05.05.2003, 08:09:06
    **/
    function col($attr=null,$data=""){
    $output="
    <td>".$data."</td>
    ";
    return $output;
    }

    /**
    * creates list in content
    * @param array $attr=null attributes array
    * @param string $data="" input data
    * @return string
    * @version 1.0
    * @author Armando Ota <armando.ota@rs-pi.com>
    * @since 05.05.2003, 08:09:06
    **/
    function list($attr=null,$data=""){
    $output="
    <table cellspacing=\"0\" cellpadding=\"0\" border=\"0\">
    ".$data."
    </table>
    ";
    return $output;
    }

    /**
    * creates list item in content
    * @param array $attr=null attributes array
    * @param string $data="" input data
    * @return string
    * @version 1.0
    * @author Armando Ota <armando.ota@rs-pi.com>
    * @since 05.05.2003, 08:09:06
    **/
    function listItem($attr=null,$data=""){
    $output="
    <tr>
    <td><img src=\"dsg/list_bullet.gif\" border=\"0\" alt=\"\" /></td><td>".$data."</td>
    </tr>
    ";
    return $output;
    }


    /**
    * parser for input string
    * @param string $str string for parsing
    * @return string
    * @version 1.1
    * @author Armando Ota <armando.ota@rs-pi.com>
    * @since 28.04.2003, 08:43:57
    **/
    function parseText($str=""){
    if($str){
    $str=" ".$str;
    $start_tag_lok=strpos($str,"<".MYTAG_PREFIX.MYTAG_SEPARATOR);
    while($start_tag_lok){

    $def_tag_len=strlen("<".MYTAG_PREFIX.MYTAG_SEPARATOR); //length of start string tag
    $start_tag_lok_end=strpos($str,">",$start_tag_lok+1); // find the closing gap for start tag

    //find tag string
    $tag_string=substr($str,$start_tag_lok+$def_tag_len,$start_tag_lok_end-($start_tag_lok+$def_tag_len));

    // posibility that tag has been written like <tag='something'>
    $start_attr_eq=strpos($tag_string,"="); //find occuarance of '=' sign
    $start_attr_space=strpos($tag_string," "); //find occuarance of ' '(whitespace) sign
    if(!$start_attr_space) $start_attr_space=strlen($tag_string);

    if($start_attr_eq<$start_attr_space){
    //if tag conatins equal sign
    $tag_name=substr($tag_string,0,$start_attr_eq);
    $attr_str=trim(substr($tag_string,0,strlen($tag_string)+1));
    //puts it into array
    $attr_arr=explode(" ",$attr_str);
    for($i=0;$i<sizeof($attr_arr);$i++){
    $sec_attr_arr=explode("=",$attr_arr[$i]);
    //attr_stack now has attribute name as key and its value
    $attr_stack[$sec_attr_arr[0]]=trim(substr($sec_attr_arr[1],1,strlen($sec_attr_arr[1])-2));
    }
    } else {
    //defines where tag name ends
    if($start_attr_space<$start_attr_eq){
    $start_attr_pos=$start_attr_space;
    } else {
    $start_attr_pos=$start_attr_eq;
    }
    //pick tagname
    $tag_name=substr($tag_string,0,$start_attr_pos);
    //gets attributes string
    $attr_str=trim(substr($tag_string,$start_attr_pos+1,strlen($tag_string)-$start_attr_pos+1));
    //puts it into array
    $attr_arr=explode(" ",$attr_str);
    for($i=0;$i<sizeof($attr_arr);$i++){
    $sec_attr_arr=explode("=",$attr_arr[$i]);
    //attr_stack now has attribute name as key and its value stored
    $attr_stack[$sec_attr_arr[0]]=trim(substr($sec_attr_arr[1],1,strlen($sec_attr_arr[1])-2));
    }
    }

    //data between start and end tags
    $end_tag_lok=strpos($str,"</".MYTAG_PREFIX.MYTAG_SEPARATOR.$tag_name.">");
    if($end_tag_lok){
    $tag_data=substr($str,$start_tag_lok_end+1,$end_tag_lok-$start_tag_lok_end-1);
    $end_tag_len=strlen("</".MYTAG_PREFIX.MYTAG_SEPARATOR.$tag_name.">");
    //builds the whole string (start and end tag) for replacement
    $whole_tag_string=substr($str,$start_tag_lok,($end_tag_lok+$end_tag_len)-$start_tag_lok);
    if(method_exists($this,$tag_name)){ //checks if tag method exists and calls it
    $str=str_replace($whole_tag_string,call_user_func(array("myTag",$tag_name),$attr_stack,$this->parseText($tag_data)),$str);
    } else {
    $str=str_replace($whole_tag_string,$this->parseText($tag_data),$str); //if tag method does not exists then we replace tag with text
    }
    }
    $start_tag_lok=strpos($str,"<".MYTAG_PREFIX.MYTAG_SEPARATOR,$start_tag_lok+1);
    }
    if($this->CONVERT_TO_BR){
    $str=str_replace(chr(10),"<br />",$str);
    }
    }
    return $str;
    }
    }



    ?>
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Miscellaneous Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    MISCELLANEOUS CODE ARTICLES

    - Creating a Web Page Controller with the HMVC...
    - Coding Controllers and Views for the HMVC De...
    - A Sample Web Application with the HMVC Desig...
    - Adding a Class to Parse Views to an HMVC Des...
    - Building a Model Class for the HMVC Design P...
    - Filtering Input Data and Generating HTML For...
    - The HMVC Design Pattern: Working with MySQL ...
    - Dispatching Requests to MVC Triads with the ...
    - Implementing the Hierarchical Model-View-Con...
    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 11 - Follow our Sitemap