Location: PHPKode > scripts > Edit File Tags > edit-file-tags/EditFileTags.class.php
<?php
  /**
   * Class to manipulate text files.
   *
   */
  class EditFileTags {
    
    /**
     * File for manipulation
     *
     * @var String
     */
    private $file = NULL;
    
    /**
     * Contents of the file
     *
     * @var String
     */
    private $string = NULL;
    
    /**
     * Texts of errors
     *
     * @var mixed
     */
    private $errors = array(
              1 => 'Error occurred during the reading of the file!'
            );
    
    /**
     * Last error id
     *
     * @var Int
     */
    private $error = false;
    
    /**
     * Setting standards of class
     * * The standard is
     * * array('tagleft' => '<!', 'tagright' => '!>');
     *
     * @var mixed
     */
    private $config = array();
    
    /**
     * Tags of the file
     *
     * @var mixed
     */
    private $tags = array();
    
    /**
     * Positions of the tags on file
     *
     * @var mixed
     */
    private $tags_positions = array();
    
    /**
     * Constructor
     *
     * @param String $file
     * @param mixed $config
     */
    public function __construct($file, $config = array()) {
      $this->file = $file;
      $this->config = $config;
      
      $this->setConfigs();
      $this->readFile();
      $this->readTags();
    }
    
    /**
     * Save and return the tags with your names
     *
     * @return mixed
     */
    public function read() {
      //Save tags
      for ($i = 0; $i < count($this->tags_positions); $i++) {
        if (isset($this->tags_positions[$i]['left']) && isset($this->tags_positions[$i]['right'])) {
          $strlen = $this->tags_positions[$i]['right'] - @$this->tags_positions[$i]['left'];
          $this->tags[$i] = substr($this->string, $this->tags_positions[$i]['left'], $strlen);
        }
      }
      return $this->tags;
    }
    
    /**
     * Write the values of thags
     *
     * @param mixed $values
     */
    public function write($values) {
      $this->read();
      $string = $this->string;
      for ($i = 0; $i < count($values); $i++) {
        $tag = $this->config['tagleft'] . $this->tags[$i] . $this->config['tagright'];
        $string = str_replace($tag, $values[$i], $string);
      }
      return $string;
    }
    
    /**
     * Read the file and stores its contents
     *
     * @return boolean
     */
    private function readFile() {
      if (!$this->string = @file_get_contents($this->file)) {
        $this->error = 1;
        return false;
      } else {
        return true;
      }
    }
    
    /**
     * Read tags from the text
     *
     */
    private function readTags() {
      //Starts the initials offsets
      $offsetleft  = 0;
      $offsetright = 0;
      //Index of ocurrence
      $ocurrences = 0;
      //Search positions of the left tags
      while($positionleft = strpos($this->string, $this->config['tagleft'], $offsetleft)) {
        $this->tags_positions[$ocurrences]['left'] = $positionleft + 2;
        $ocurrences++;
        $offsetleft = ++$positionleft;
      }
      //Index of ocurrence
      $ocurrences = 0;
      //Search positions of the right tags
      while($positionright = strpos($this->string, $this->config['tagright'], $offsetright)) {
        $this->tags_positions[$ocurrences]['right'] = $positionright;  
        $ocurrences++;
        $offsetright = ++$positionright;
      }
    }
    
    private function setConfigs() {
      //Configure the tags for read
      (isset($this->config['tagleft']))  ? $this->config['tagleft']  = $this->config['tagleft']  : $this->config['tagleft']  = '<!';
      (isset($this->config['tagright'])) ? $this->config['tagright'] = $this->config['tagright'] : $this->config['tagright'] = '!>';
    }
    
    /**
     * Returns the last error
     *
     * @return String
     */
    public function getLastError() {
      if ($this->error != 0) {
        return $this->errors[$this->error];
      } else {
        return false;
      }
    }
    
  }
?>
Return current item: Edit File Tags