Welcome Guest, Not a member yet? Register   Sign In
Anyone have a library to parse users input for variables or macros?
#1

(This post was last modified: 09-27-2015, 03:47 PM by jLinux.)

In the application I'm in the middle of developing, there are many instances where it would be useful for a user to be able to use "variables" in the input of whatever is being created.

For example, admins/moderators can create "pages" in the application, and lets say they wanted to display the name of the page inside the page content, it would be useful if they could just put something like {{page_title}}, and at runtime, have the name of the page be displayed.


If every instance of where these "variables" would be used, they were just string to string replacements, (such as my example above), then a simple preg_replace or str_replace would suffice... However, I need something a little more complicated.

Lets say they want to do something like display the email address of the account with the username demouser, then they should be able to use something similar to {{demouser[email]}}

Some other examples of what would/could be used...
  • {{app_setting[status]}} - Status of application
  • {{username}} - Username of account viewing
  • {{john_doe[session_id]}} - Current session ID for John Doe
  • {{{{username}}[email]}} - Email address of the account viewing said variable, (Hoping for the ability of var-vars)
Does anyone know of an existing library that can accomplish this? If so, let me know! That would be a huge help. If not, Im sure ill get by with just a lot of regex and something similar to this (after updating it) 

Thank you
Reply
#2

(This post was last modified: 09-27-2015, 05:01 PM by jLinux.)

I messed around for a bit and came up with something simple, pasted below. If anyone has something better or more stable, then let me know please.. Or something specific to CI.

PHP Code:
<?php
class Macro_parser {
    private static 
$_paragraph;
    private static 
$_criteria;

    public function 
__construct($paragraph$criteria)
    {
        static::
$_paragraph $paragraph;
        static::
$_criteria  $criteria;

        static::
_parse();
    }

    static private function 
_parse()
    {
        
preg_replace_callback(
            
"/\{{3}([a-z]+\[?\]?.+)\}{3}/U",
            
"static::_replace_macro",
            static::
$_paragraph);
    }

    static private function 
_replace_macro($match)
    {
        
$macro $match[0];
        
$text $match[1];

        
// See if this macro is an array
        
if(preg_match('/(.*?)\[(.*)\]/'$text$array))
        {
            
// Check that the array name is in the criteria, and that its an array
            
if(isset( static::$_criteria$array[1] ] ) AND is_array(  static::$_criteria$array[1] ]))
            {
                
// Make sure the value is in the criteria arrays array..
                
if(isset(static::$_criteria$array[1] ][ $array[2]]))
                {
                    
$new_macro str_replace(['[',']'],['\[','\]'], $macro);

                    static::
$_paragraph preg_replace("/{$new_macro}/", static::$_criteria$array[1] ][ $array[2]], static::$_paragraph);
                }
                else
                {
                    die(
"The macro {$text} is supposed to be an array, but its not found in the criteria as an array\n");
                }
            }
            else
            {
                die(
"The macro {$text} is not found int he criteria..\n");
            }
        }
        
// Not an array, just a simple string replacement
        
elseif(isset(static::$_criteria[$text]))
        {
            static::
$_paragraph preg_replace("/{$macro}/", static::$_criteria[$text], static::$_paragraph);
        }
        else
        {
            die(
"The macro {$text} wasn't found in the criteria...\n");
        }
    }

    public function 
__toString()
    {
        return (string)static::
$_paragraph;
    }
}

$criteria = array(
    
'first_name'    => 'John',
    
'last_name'     => 'Doe',
    
'age'           => '30',
    
'income'        => '\$120k',
    
'profession'    => 'Linux Engineer',
    
'favorites'     => array(
        
'car'   => 'Camaro SS',
        
'pet'   => 'Dexter',
        
'OS'    => 'Linux'
    
)
);


$paragraph = <<<EOF
Hello! My name is {{{first_name}}} {{{last_name}}}, I am {{{age}}} years old,
and make {{{income}}} a year as a {{{profession}}}. My favorite car is {{{favorites[car]}}},
my favorite Operating System is {{{favorites[OS]}}}, and my favorite pet is {{{favorites[pet]}}}.

Thank you,
- {{{first_name}}} {{{last_name}}}

EOF;

echo new 
Macro_parser($paragraph$criteria);


/**
 * Result...
Hello! My name is John Doe, I am 30 years old,
and make $120k a year as a Linux Engineer. My favorite car is Camaro SS,
my favorite Operating System is Linux, and my favorite pet is Dexter.

Thank you,
- John Doe
*/ 
http://code.linuxdigest.org/view/8cbdd803
Reply
#3

You could probably adapt a template library (like Twig, Smarty, or Plates) for your purposes.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB