Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • jooservices Friend
    #169734

    Somehow you are working with module and plugin for Joomla and want to use some special format code as params. Like

    {yoogallery src= style=}

    I’ve my own class to phrase these regex in easier way.

    <?php
    /**
    * @name JOOframework
    * @version 1.0.0 Aug 26 2011
    * @package Joomla plugin
    * @subpackage System
    * @author Viet Vu
    * @copyright (C) 2009 - 2011 by JOOservices Ltd Company - All rights reserved!
    * @license GNU/GPL, see LICENSE.php
    * @link http://jooservices.com
    * @uses
    * This's JOOregular expression class.
    */

    /**
    * OWNER %%LICENSED_TO%%
    */

    // Check to ensure this file is included in Joomla!
    defined ( '_JEXEC' ) or die ( 'Restricted access' );
    ?>
    <?php

    /**
    * Extends JOOFregexBase with JOOpattern
    */
    class JOOFregex extends JOOFregexBase {

    private $blocks;
    /**
    * Construct method
    * @param string regex prefix
    */
    public function __construct($regPrefix = 'joo') {
    parent::__construct ( '/{' . $regPrefix . '.*}/' );
    }

    /**
    * Method to load source text
    * @param string
    * @param bool Reset current data
    * @return array object Array of blocks
    */
    public function load($text, $reset = false) {
    parent::load ( $text );
    if (parent::getTotal () > 0) {
    foreach ( parent::getList () as $block ) {
    $data = new JOOFregexData ( $block );
    $this->blocks [] = $data;
    }
    }
    return $this->getBlocks ( $reset );
    }

    /**
    * Method to get blocks
    */
    public function getBlocks($reset = false) {
    return $this->blocks ;
    }

    }

    /**
    * Extends JOOFregexBase for a JOOregex data
    */
    class JOOFregexData extends JOOFregexBase {
    private $data;
    private $registry = '';

    /**
    * Construct method
    * @param string data source
    */
    public function __construct($source) {
    parent::__construct ( '#(w+)=[(.*?)]#' );
    parent::load ( $source );
    $this->load ();
    }

    /**
    * Method to parse data source into array
    * @return object JParameter
    */
    public function load() {
    if (parent::getTotal () > 0) {
    $index = 0;
    foreach ( parent::getList () as $data ) {
    $name = $this->matches [1] [$index];
    $value = $this->matches [2] [$index];
    $this->data [$name] = $value;
    /* registry storing datas in string type allow use in Joomla as JParamater */
    $this->registry .= $name . '=' . $value . "rn";
    $index ++;
    }
    }
    }

    /**
    * Method to return raw array data
    */
    public function getList() {
    return $this->data;
    }

    /**
    * Method to get data
    * @param string dataname
    * @param mixed default value if have no data
    * @return mixed
    */
    public function get($name, $def = false) {
    if (! isset ( $this->data [$name] ))
    return $def;
    return $this->data [$name];
    }

    /**
    * Method to set data value
    * @param string dataname
    * @param mixed value
    */
    public function set($name, $value) {
    $this->data [$name] = $value;
    }
    }

    /**
    * Abstract class
    * Base class use for general regular expression functions
    */
    abstract class JOOFregexBase {

    protected $matches;

    private $text; /* text source */
    private $pattern = '';
    private $total;

    /**
    * Method construct
    * @param str regular expression
    */
    protected function __construct($pattern = 'joo') {
    $this->pattern = $pattern;
    }

    /**
    * Method to load text into class and process preg
    * @return int total matches
    */
    protected function load($text) {
    $this->text = $text;
    $this->total = preg_match_all ( $this->pattern, $text, $this->matches );
    return $this->total;
    }

    /**
    * Method to get source text
    */
    public function getText() {
    return $this->text;
    }

    /**
    * Method to get full pattern matches
    */
    public function getList() {
    /* Orders results so that $matches[0] is an array of full pattern matches, $matches[1] is an array of strings matched by the first parenthesized subpattern, and so on. */
    return $this->matches [0];
    }

    /**
    * Method to get total matches
    */
    public function getTotal() {
    return $this->total;
    }

    }

    ?>

    How to use:


    $this->_regex = new JOOFregex ( 'highslide' );
    $this->_regex->load ( $this->_body );

    That’s all for declare.
    And after that you can use it very simply


    $blocks = $this->_regex->getBlocks ();
    if ( $blocks ) {
    if ( is_array($blocks)) {
    foreach ( $blocks as $block ) {

    }
    }

    }

    Each block is a “whole format string” . And you can use it like

    $block->get($name,$defaultValue)
    ( you can use set also )

    And finally of course you want to replce this format string by your new html just

    $html = str_replace($block->getText(),$newHTML,$html);

    Very simply , rite ?

    fri13th Friend
    #419802

    thanks for sharing man!

Viewing 2 posts - 1 through 2 (of 2 total)

This topic contains 2 replies, has 2 voices, and was last updated by  fri13th 13 years, 1 month ago.

We moved to new unified forum. Please post all new support queries in our New Forum