Pages

Showing posts with label Smarty Development. Show all posts
Showing posts with label Smarty Development. Show all posts

Tuesday, January 10, 2012

Convert BBCode to HTML in PHP

function bb2html($message)
        {                               
            $preg = array(
                '/(?<!\\\\)\[COLOR(?::\w+)?=(.*?)\](.*?)\[\/COLOR(?::\w+)?\]/si'   => "<span style=\"color:\\1\">\\2</span>",
                '/(?<!\\\\)\[SIZE(?::\w+)?=(.*?)\](.*?)\[\/SIZE(?::\w+)?\]/si'     => "<span style=\"font-size:\\1\">\\2</span>",
                '/(?<!\\\\)\[FONT(?::\w+)?=(.*?)\](.*?)\[\/FONT(?::\w+)?\]/si'     => "<span style=\"font-family:\\1\">\\2</span>",
                '/(?<!\\\\)\[ALIGN(?::\w+)?=(.*?)\](.*?)\[\/ALIGN(?::\w+)?\]/si'   => "<div style=\"text-align:\\1\">\\2</div>",
                '/(?<!\\\\)\[B(?::\w+)?\](.*?)\[\/B(?::\w+)?\]/si'                 => "<span style=\"font-weight:bold\">\\1</span>",
                '/(?<!\\\\)\[I(?::\w+)?\](.*?)\[\/I(?::\w+)?\]/si'                 => "<span style=\"font-style:italic\">\\1</span>",
                '/(?<!\\\\)\[U(?::\w+)?\](.*?)\[\/U(?::\w+)?\]/si'                 => "<span style=\"text-decoration:underline\">\\1</span>",
                '/(?<!\\\\)\[CENTER(?::\w+)?\](.*?)\[\/CENTER(?::\w+)?\]/si'       => "<div style=\"text-align:center\">\\1</div>",
                               
                // [email]
                '/(?<!\\\\)\[EMAIL(?::\w+)?\](.*?)\[\/EMAIL(?::\w+)?\]/si'         => "<a href=\"mailto:\\1\" class=\"bb-email\">\\1</a>",
                '/(?<!\\\\)\[EMAIL(?::\w+)?=(.*?)\](.*?)\[\/EMAIL(?::\w+)?\]/si'   => "<a href=\"mailto:\\1\" class=\"bb-email\">\\2</a>",
                // [url]
                '/(?<!\\\\)\[URL(?::\w+)?\]www\.(.*?)\[\/URL(?::\w+)?\]/si'        => "<a href=\"http://www.\\1\" target=\"_blank\" class=\"bb-url\">\\1</a>",
                '/(?<!\\\\)\[URL(?::\w+)?\](.*?)\[\/URL(?::\w+)?\]/si'             => "<a href=\"\\1\" target=\"_blank\" class=\"bb-url\">\\1</a>",
                '/(?<!\\\\)\[URL(?::\w+)?=(.*?)?\](.*?)\[\/URL(?::\w+)?\]/si'      => "<a href=\"\\1\" target=\"_blank\" class=\"bb-url\">\\2</a>",
                // [img]
                '/(?<!\\\\)\[IMG(?::\w+)?\](.*?)\[\/IMG(?::\w+)?\]/si'             => "<img src=\"\\1\" alt=\"\\1\" class=\"bb-image\" />",
                '/(?<!\\\\)\[IMG(?::\w+)?=(.*?)x(.*?)\](.*?)\[\/IMG(?::\w+)?\]/si' => "<img width=\"\\1\" height=\"\\2\" src=\"\\3\" alt=\"\\3\" class=\"bb-image\" />",
                                       
                // [list]
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\*(?::\w+)?\](.*?)(?=(?:\s*<br\s*\/?>\s*)?\[\*|(?:\s*<br\s*\/?>\s*)?\[\/?LIST)/si' => "\n<li class=\"bb-listitem\">\\1</li>",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\/LIST(:(?!u|o)\w+)?\](?:<br\s*\/?>)?/si'    => "\n</ul>",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\/LIST:u(:\w+)?\](?:<br\s*\/?>)?/si'         => "\n</ul>",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\/LIST:o(:\w+)?\](?:<br\s*\/?>)?/si'         => "\n</ol>",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[LIST(:(?!u|o)\w+)?\]\s*(?:<br\s*\/?>)?/si'   => "\n<ul class=\"bb-list-unordered\">",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[LIST:u(:\w+)?\]\s*(?:<br\s*\/?>)?/si'        => "\n<ul class=\"bb-list-unordered\">",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[LIST:o(:\w+)?\]\s*(?:<br\s*\/?>)?/si'        => "\n<ol class=\"bb-list-ordered\">",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[LIST(?::o)?(:\w+)?=1\]\s*(?:<br\s*\/?>)?/si' => "\n<ol class=\"bb-list-ordered,bb-list-ordered-d\">",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[LIST(?::o)?(:\w+)?=i\]\s*(?:<br\s*\/?>)?/s'  => "\n<ol class=\"bb-list-ordered,bb-list-ordered-lr\">",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[LIST(?::o)?(:\w+)?=I\]\s*(?:<br\s*\/?>)?/s'  => "\n<ol class=\"bb-list-ordered,bb-list-ordered-ur\">",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[LIST(?::o)?(:\w+)?=a\]\s*(?:<br\s*\/?>)?/s'  => "\n<ol class=\"bb-list-ordered,bb-list-ordered-la\">",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[LIST(?::o)?(:\w+)?=A\]\s*(?:<br\s*\/?>)?/s'  => "\n<ol class=\"bb-list-ordered,bb-list-ordered-ua\">",
                                       
                //line breaks
                '/\n/'                                                               => "<br>",
                // escaped tags like \[b], \[color], \[url], ...
                '/\\\\(\[\/?\w+(?::\w+)*\])/'                                      => "\\1"
                               
            );
                               
            $message = preg_replace(array_keys($preg), array_values($preg), $message);
            return $message;
        }


// Call Function bb2html and echo

$htmltext = bb2html($message);
echo $htmltext;

Friday, August 5, 2011

Find string between two strings - PHP

This is a little function to find a string between two specified pieces of strings. This could be used to find any string between two strings.

function get_string($string, $start, $end){
 $string = " ".$string;
 $pos = strpos($string,$start);
 if ($pos == 0) return "";
 $pos += strlen($start);
 $len = strpos($string,$end,$pos) - $pos;
 return substr($string,$pos,$len);
}

$fullstring = "Find string between two strings in PHP";
$parsed = get_string($fullstring, "between ", " in PHP");

echo $parsed; 
(result = two strings)

Thursday, August 4, 2011

Assign an smarty variable to a PHP variable

Developer can use PHP code in smarty(.tpl file code) within a {php} block or an {include_php}.

when developer use a {php} block inside a Smarty template developer are inside the Smarty scope (object)
So when
developer need to reference a Smarty method developer would use $this
so when developer assign an smarty variable to a php variable it looks something like this in smarty:

Code:
{php}
   $t = $this->get_template_vars( 'foo' );
{/php}

print_r in Smarty development

{$foo|@print_r}

The @ tells smarty to pass the entire array to the modifier rather than calling the modifier for each element of the array. 


Smarty also print array like php print_r() function: {$foo|@debug_print_var}

Developer may also consider smarty's debug facility (which developer can enable in-template by inserting a {debug} tag).