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

[eluser]Reynolds[/eluser]
[quote author="umefarooq" date="1294235849"]nice thread im also looking for code or library which can work like wordpress short codes which can help users to and tags from backend and in front end it become html or your desired output. here is one library which can help you more

https://bitbucket.org/dhorrigan/simpletags/src[/quote]
Thanks a million umefarooq! :-)
#12

[eluser]Phil Sturgeon[/eluser]
[quote author="umefarooq" date="1294235849"]nice thread im also looking for code or library which can work like wordpress short codes which can help users to and tags from backend and in front end it become html or your desired output. here is one library which can help you more

https://bitbucket.org/dhorrigan/simpletags/src[/quote]

You beat me to it. +1 for this, been using it a while and its an excellent tag parser.
#13

[eluser]umefarooq[/eluser]
yes its a nice library but here is my question how we can use this library work like wordpress shortcodes, which can be really handy for user or designer for any themes check out this theme using shortcodes

http://prosto.cult-f.net/skins/shortcodes/

this tutorial how to use shortcode in wordpress

http://net.tutsplus.com/tutorials/wordpr...right-way/
#14

[eluser]diZzyCoDeR[/eluser]
It's been a month, but maybe this will help others...

It *IS* indeed a great library, and it *DOES* come with a function to do what you're asking. I missed it completely until I was rifling through it to steal code to write myself a function that would simply convert the data stored in a variable
Code:
$content = '{foo} and {bar}';
to the values stored in
Code:
$foobar['foo'] = 'FOO';
$foobar['bar'] = 'BAR';

...then I saw it, parse_globals() -- which you could use on it's own, or part of the Simpletags library, as such:

Code:
$this->load->library('simpletags');

$output = $this->simpletags->parse_globals($content, $foobar);

thus, $output will evaluate to:
Quote:FOO and BAR

Code:
/**
* Parse Globals
*
* Parses global data tags.  These are tags that do not use a trigger
* and have a variable in the $data array.  This enables you to use
* globals inside of other tags:
*
* The Tag:
* {tag:blog:posts offset="{offset}"}
*
* The data array:
* array(
*     'offset' => $this->uri->segment(3),
* );
*
* @access    public
* @param    string    The content to parse
* @param    array    The globals
* @return    string    The parsed content
*/
public function parse_globals($content, $data)
{
        foreach ($data as $var => $value)
        {
                if ( ! is_array($value))
                {
                        $content = str_replace('{'.$var.'}', $value, $content);
                }
        }
        return $content;
}
#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]
#16

[eluser]Reynolds[/eluser]
Nice! Keep it coming :-) Good thing I keep track of this thread.




Theme © iAndrew 2016 - Forum software by © MyBB