Welcome Guest, Not a member yet? Register   Sign In
Parser->Parser_String
#1

[eluser]tonydewan[/eluser]
This is a quicky. I'm sure someone has written this before, but I couldn't find anything in the forums after a few seconds of searching. Basically, I added a parse_string() method to the built in parser. This way, you can take a string of text(like say, from a DB) and parse out the same pseudo-variables. I went ahead and made it default to return a string (rather than appending to output, like the normal parse() method), because that made the most sense to me. This functionality would be welcome in the main library, I think.

Here is MY_Parser.php

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Parser extends CI_Parser {

    /**
     *  Parse a string
     *
     * Parses pseudo-variables contained in the specified template,
     * replacing them with the data in the second param
     *
     * @access    public
     * @param    string
     * @param    array
     * @param    bool
     * @return    string
     */
    function parse_string($string, $data, $return = TRUE)
    {
        $CI =& get_instance();
        
        if ($string == '') return FALSE;
        
        foreach ($data as $key => $val)
        {
            if (is_array($val))
            {
                $string = $this->_parse_pair($key, $val, $string);        
            }
            else
            {
                $string = $this->_parse_single($key, (string)$val, $string);
            }
        }

        if ($return == FALSE) $CI->output->append_output($string);
        
        return $string;
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB