Welcome Guest, Not a member yet? Register   Sign In
string parsing in between curly bracket
#15

[eluser]diZzyCoDeR[/eluser]
Furthermore, I'm using both the methods and it took me a bit to figure out how, so here's the gist:

Code:
// parse the db field using simpletags
$this->load->library('simpletags');

// may or may not contain a {date:display format="%M %d, %Y"}{now}{/date:display} tag
if (preg_match('/{date.*?:/i', $desc) != 0) {

    // {date: will be our 'trigger' to start parsing
    $this->simpletags->set_trigger('date');

    // set up a value for replacing anything between our {date:display} tags {/date:display}
    $date_data = array('now' => time());

    // parse that bad boy and call our helper function from MY_date_helper.php
    $desc = $this->simpletags->parse($desc, $date_data, 'format_timestamp');

    // above, the array is returned, here we'll grab the 'content' which is the goodies
    $desc = $desc['content'];
}
// I continue to parse for other tags from my $desc
$desc = $this->simpletags->parse_globals($desc, $data);

and my little callback function:

Code:
/** << slap this in MY_date_helper.php >>
* Convert UNIX TIMESTAMP DATE to a HUMAN readable date via mdate
*
* Returns the HUMAN DATE equivalent of a given UNIX TIMESTAMP
*
* enjoy AT YOUR OWN RISK, no warranty, no charge, no credit necessary
*
* @author    David Touchette <http://david.touchette.com/>
* @access    public
* @param     array   -- specifically an array returned by simpletags->parse (via @dhorrigan)
* @return    string  -- [content] of the {tag} formated via mdate (see CI manual)
*/

if( ! function_exists('format_timestamp'))
{
    function format_timestamp($data = array()) {
        if ( empty($data) ) {
            return false;
        }

        // we expect a "format" attribute, but we could always do a default of %d/%m/%y
        if ( array_key_exists('format', $data['attributes']) ) {
            // from our data, we're expecting a MySQL-style format that mdate loves
            return mdate($data['attributes']['format'], $data['content']);
        } else {
            return false;
        }
    }
}

and some output fo yo ass...

[quote]
Array (
[content] => On Feb 16, 2011 {name} did some stuff at {place} and thought it was {rating}.
[tags] => Array (
[0] => Array (
[full_tag] => {date:display format="%M %d, %Y"}1297916387{/date:display}
[attributes] => Array (
[format] => %M %d, %Y
)
[full_segments] => :display
[segments] => Array (
[0] =>
[1] => display
)
[content] => 1297916387
[marker] => marker_0k0dj3j4nJHDj22j
)
)
)
[quote]


Messages In This Thread
string parsing in between curly bracket - by El Forum - 01-04-2011, 07:08 PM
string parsing in between curly bracket - by El Forum - 01-04-2011, 08:35 PM
string parsing in between curly bracket - by El Forum - 01-04-2011, 08:44 PM
string parsing in between curly bracket - by El Forum - 01-04-2011, 08:59 PM
string parsing in between curly bracket - by El Forum - 01-04-2011, 09:07 PM
string parsing in between curly bracket - by El Forum - 01-04-2011, 09:16 PM
string parsing in between curly bracket - by El Forum - 01-04-2011, 09:25 PM
string parsing in between curly bracket - by El Forum - 01-04-2011, 09:33 PM
string parsing in between curly bracket - by El Forum - 01-04-2011, 10:16 PM
string parsing in between curly bracket - by El Forum - 01-05-2011, 01:57 AM
string parsing in between curly bracket - by El Forum - 01-05-2011, 02:00 AM
string parsing in between curly bracket - by El Forum - 01-05-2011, 05:06 AM
string parsing in between curly bracket - by El Forum - 01-06-2011, 01:09 AM
string parsing in between curly bracket - by El Forum - 02-16-2011, 08:19 PM
string parsing in between curly bracket - by El Forum - 02-16-2011, 10:25 PM
string parsing in between curly bracket - by El Forum - 02-18-2011, 07:09 AM



Theme © iAndrew 2016 - Forum software by © MyBB