Source for file class.string_tags.php

Documentation is available at class.string_tags.php

  1. <?php
  2.  
  3. /****************************************************************************
  4.  * Software:    Tag Extraction Class
  5.  *              extracts the tags and corresponding text from a string
  6.  * Version:        1.1
  7.  * Date:        2005/12/08
  8.  * Author:        Bintintan Andrei, <andy@interpid.eu>
  9.  *
  10.  * Last Modification: 2007/12/07
  11.  * 
  12.  * License: Free for Non Commercial use. 
  13.  *          You may not sell or use this class in a Commercial Project without written permission from the Author. 
  14.  * 
  15.  * If you like my work please DONATE!!! **** paypal@interpid.eu ***** 
  16.  * 
  17.  * For more classes visit ***** www.interpid.eu ***** 
  18.  * 
  19.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR “AS IS” AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
  20.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE 
  21.  * AUTHOR OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
  22.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
  23.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
  24.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
  25.  * SUCH DAMAGE.
  26.  * 
  27.  * PLEASE REPORT ANY BUGS TO THE AUTHOR. 
  28.  * 
  29.  * THANK YOU. 
  30.  * 
  31.  */
  32.  
  33. /**
  34.  * Extracts the tags and corresponding text from a string
  35.  * 
  36.  * @name     string_tags
  37.  * @author     Bintintan Andrei <andy@interpid.eu>
  38.  * @version 1.1
  39.  */
  40. class string_tags{
  41.  
  42. /**
  43.  * Contains the Tag/String Correspondence
  44.  *
  45.  * @access     protected
  46.  * @var        struct(array) 
  47.  */
  48. protected $aTAGS = array();
  49.  
  50. /**
  51.  * Contains the links for the tags that have specified this parameter
  52.  * 
  53.  * @access     protected
  54.  * @var        struct(array) 
  55.  */
  56. protected $aHREF;
  57.  
  58. /**
  59.  * The maximum number of chars for a tag
  60.  *
  61.  * @access     protected
  62.  * @var        integer 
  63.  */
  64. protected $iTagMaxElem;
  65.     
  66.     /**
  67.      * Constructor
  68.      *
  69.      * @access     public
  70.      * @param    numeric $p_tagmax - the number of characters allowed in a tag
  71.      * @return    void 
  72.      */
  73.     public function __construct($p_tagmax 10){
  74.         $this->aTAGS = array();
  75.         $this->aHREF = array();
  76.         $this->iTagMaxElem = $p_tagmax;
  77.     }
  78.  
  79.     /**
  80.      * Returns TRUE if the specified tag name is an "<open tag>", (it is not already opened)
  81.      *
  82.      * @access     protected
  83.      * @param    string $p_tag - tag name
  84.      * @param    array $p_array - tag arrays
  85.      * @return    boolean 
  86.      */
  87.     protected function OpenTag($p_tag$p_array){
  88.  
  89.         $aTAGS $this->aTAGS;
  90.         $aHREF $this->aHREF;
  91.         $maxElem $this->iTagMaxElem;
  92.       
  93.         if (!eregi("^<([a-zA-Z1-9]{1,$maxElem}) *(.*)>$"$p_tag$reg)) return false;
  94.  
  95.         $p_tag $reg[1];
  96.  
  97.         $sHREF array();
  98.         if (isset($reg[2])) {
  99.             preg_match_all("|([^ ]*)=[\"'](.*)[\"']|U"$reg[2]$outPREG_PATTERN_ORDER);
  100.             for ($i=0$i<count($out[0])$i++){
  101.                 $out[2][$ieregi_replace("(\"|')"""$out[2][$i]);
  102.                 array_push($sHREFarray($out[1][$i]$out[2][$i]));
  103.             }           
  104.         }
  105.  
  106.         if (in_array($p_tag$aTAGS)) return false;//tag already opened
  107.  
  108.         if (in_array("</$p_tag>"$p_array)) {
  109.             array_push($aTAGS$p_tag);
  110.             array_push($aHREF$sHREF);
  111.             return true;
  112.         }
  113.         return false;
  114.     }//OpenTag
  115.  
  116.     /** returnes true if $p_tag is a "<close tag>"
  117.         @param     $p_tag - tag string
  118.                 $p_array - tag array;
  119.         @return true/false
  120.     */
  121.     /**
  122.      * Returns true if $p_tag is a "<close tag>"
  123.      *
  124.      * @access     protected
  125.      * @param    sting $p_tag - tag name
  126.      * @param    array $p_array - tag array
  127.      * @return    boolean 
  128.      */
  129.     protected function CloseTag($p_tag$p_array){
  130.  
  131.         $aTAGS $this->aTAGS;
  132.         $aHREF $this->aHREF;
  133.         $maxElem $this->iTagMaxElem;
  134.  
  135.         if (!ereg("^</([a-zA-Z1-9]{1,$maxElem})>$"$p_tag$reg)) return false;
  136.  
  137.         $p_tag $reg[1];
  138.  
  139.         if (in_array("$p_tag"$aTAGS)) {
  140.             array_pop($aTAGS);
  141.             array_pop($aHREF);
  142.             return true;
  143.         }
  144.         return false;
  145.     }// CloseTag
  146.     
  147.     /**
  148.     * Expands the paramteres that are kept in Href field
  149.     * 
  150.     * @access     protected
  151.     * @param        array of parameters
  152.     * @return       string with concatenated results
  153.     */
  154.     
  155.     /**
  156.      * Expands the paramteres that are kept in Href field
  157.      *
  158.      * @access     protected
  159.      * @param    struct $pResult 
  160.      * @return    string 
  161.      */
  162.     protected function expand_parameters($pResult){
  163.         $aTmp $pResult['params'];
  164.         if ($aTmp <> '')
  165.             for ($i=0$i<count($aTmp)$i++){
  166.                 $pResult[$aTmp[$i][0]] $aTmp[$i][1];
  167.             }
  168.             
  169.         unset($pResult['params']);
  170.         
  171.         return $pResult;
  172.     }//expand_parameters
  173.     
  174.     
  175.     /**
  176.      * Optimizes the result of the tag result array
  177.      * In the result array there can be strings that are consecutive and have the same tag, they
  178.      * are concatenated.
  179.      *
  180.      * @access     protected
  181.      * @param    array $result - the array that has to be optimized
  182.      * @return    array - optimized result
  183.      */
  184.     protected function optimize_tags($result){
  185.  
  186.         if (count($result== 0return $result;
  187.  
  188.         $res_result array();
  189.         $current $result[0];
  190.         $i 1;
  191.  
  192.         while ($i count($result)){
  193.  
  194.             //if they have the same tag then we concatenate them
  195.             if (($current['tag'== $result[$i]['tag']&& ($current['params'== $result[$i]['params'])){
  196.                 $current['text'.= $result[$i]['text'];
  197.             }else{
  198.                 $current $this->expand_parameters($current);
  199.                 array_push($res_result$current);
  200.                 $current $result[$i];
  201.             }
  202.  
  203.             $i++;
  204.         }
  205.  
  206.         $current $this->expand_parameters($current);
  207.         array_push($res_result$current);
  208.         
  209.         return $res_result;
  210.     }//optimize_tags
  211.  
  212.     
  213.     
  214.        /** Parses a string and returnes the result
  215.         @param     $p_str - string
  216.         @return array (
  217.                     array (string1, tag1),
  218.                     array (string2, tag2)
  219.                 )
  220.     */
  221.        /**
  222.         * Parses a string and returnes an array of TAG - SRTING correspondent array
  223.         * The result has the following structure:
  224.         *         array(
  225.         *             array (string1, tag1),
  226.         *             array (string2, tag2),
  227.         *             ... etc
  228.         *         )
  229.         * 
  230.         * @access     public
  231.         * @param    string $p_str - the Input String
  232.         * @return    array - the result array
  233.         */
  234.     public function get_tags($p_str){
  235.  
  236.         $aTAGS $this->aTAGS;
  237.         $aHREF $this->aHREF;
  238.         $aTAGS array();
  239.         $result array();
  240.  
  241.         $reg preg_split('/(<.*>)/U'$p_str-1PREG_SPLIT_DELIM_CAPTURE);
  242.  
  243.         $sTAG "";
  244.         $sHREF "";
  245.  
  246.         while (list($key$valeach($reg)) {
  247.             if ($val == ""continue;
  248.  
  249.             if ($this->OpenTag($val,$reg)){
  250.                 $sTAG (($temp end($aTAGS)) != NULL$temp "";
  251.                 $sHREF (($temp end($aHREF)) != NULL$temp "";
  252.             }elseif($this->CloseTag($val$reg)){
  253.                 $sTAG (($temp end($aTAGS)) != NULL$temp "";
  254.                 $sHREF (($temp end($aHREF)) != NULL$temp "";
  255.             }else {
  256.                 if ($val != "")
  257.                     array_push($resultarray('text'=>$val'tag'=>$sTAG'params'=>$sHREF));
  258.             }
  259.         }//while
  260.  
  261.         return $this->optimize_tags($result);
  262.     }//get_tags
  263.  
  264. }//class string_tags
  265.  
  266. ?>

Documentation generated on Wed, 04 Feb 2009 22:01:34 +0100 by phpDocumentor 1.4.0a2