Location: PHPKode > scripts > Get tag value > get-tag-value/GTV.class.php
<?php
/***************************************************************************
============================================================================
| @ Script Name             : Get Tag Value
| @ Version                 : 1.0
| @ Description             : Get The Tag Value .
| @ All rights reserved to  : NaiF PHP
| @ Created In              : 09-07-2007
| @ Support             : hide@address.com
============================================================================
****************************************************************************/

class GTV
{

   # Public #
   var $path;
   var $start_tag;
   var $end_tag;

   # Private #
   var $tag_value;
   var $conts = "";
   var $DefineError = array(
                            " please enter the start and the end of the requested tag",
                            " the start tag must be the same as the end tag",
                            " please enter the url of the page you want request tags from ",
                            " unable to reach the requested page ",
                            " sorry .. unable to get the requested tags ",
                            " error .. the starting and ending tags must be an array "
                            );


   // to start find you tag value ...
   function GTV($path,$start_tag,$end_tag)
   {
      $this->path = $path;
      $this->start_tag = $start_tag;
      $this->end_tag = $end_tag;

      if(empty($this->start_tag) || empty($this->end_tag))
      {
         $this->tag_value = $this->DefineError[0];
      }
      else
      {
         @set_time_limit(0);
         $this->tag_value = $this->GetTagValue();
      }

     return $this->tag_value;
   }

   // fro find your tag & return value him
   // array or one tag(s)
   function GetTagValue()
   {
       if(($_cont = $this->LoadPage()) != $this->DefineError[2])
       {
          if(is_array($this->start_tag) && is_array($this->end_tag))
          {
             foreach($this->start_tag as $id=>$tag)
             {
                $ST = $this->start_tag[$id];
                $ET = $this->end_tag[$id];

                if($this->check($ST,$ET) == false)
                {
                   $__res = $this->DefineError[1];
                }
                else
                {
                   preg_match( "/$ST(.*)$ET/s", $_cont, $match );

                   $__res[] = htmlspecialchars($tag) .' :: '. (is_null($match[1]) ? $this->DefineError[4] : htmlspecialchars($match[1]));
                }
             }
          }
          else if(is_array($this->start_tag) || is_array($this->end_tag))
          {
              $_res = $this->DefineError[5];
          }
          else
          {
              if($this->check($this->start_tag,$this->end_tag) == false)
              {
                  $__res = $this->DefineError[1];
              }
              else
              {
                  preg_match( "/$this->start_tag(.*)$this->end_tag/s", $_cont, $match );

                  $__res = htmlspecialchars($this->start_tag) .' :: '. htmlspecialchars($match[1]);
              }
          }

          if($__res)
          {
              $_res = $__res;
          }
          else
          {
              $_res = $this->DefineError[4];
          }
        }
        else
        {
             $_res = $this->DefineError[2];
        }

      return $_res;
    }

   // for return the contents you file ..
   function LoadPage()
   {
         if($this->path == "")
         {
           $this->conts = $this->DefineError[2];
         }
         else
         {
            $open_file = @fopen($this->path, 'r') or die ($this->DefineError[3]);

            while( !feof ($open_file) )
            {
                $buf = trim( fgets( $open_file, 4096 ) );
                $this->conts .= $buf;
            }

            @fclose($open_file);

         }

      return $this->conts;
    }

    // for check ( Start Tag == End Tag)
    function check($st,$et)
    {
         $check_start1 = preg_replace("/<(.+)>/U","\\1",$st);
         $check_start = explode(" ",$check_start1);
         $check_end   = preg_replace("/<(.+)>/U","\\1",$et);
         $check_end = str_replace ('\\/','',$check_end);

         if(is_array($check_start))
         {
            $get_tag_name = $check_start[0];
         }
         else
         {
            $get_tag_name = $check_start1;
         }


         if($get_tag_name == $check_end)
         {
             return true;
         }
         else
         {
             return false;
         }
    }

}

// End Class :)
?>


Return current item: Get tag value