1

How can I convert {$game.name.fullname} to $game['name']['fullname'] using regex.

thanks

1
  • 3
    Are you writing your own templating engine ? Seems like rewriting the wheel a bit - depending on your point of view, PHP is a templating language itself or there's Smarty or other existing solutions already. Commented Mar 14, 2011 at 14:14

4 Answers 4

1
preg_replace(
    '/{\$([^.{}]*)\.([^.{}]*)\.([^.{}]*)}/',
    '$$1[\'$2\'][\'$3\']',
    '{$game.name.fullname}');

Edit: Edited according to ridgerunner's comment. Now matches anything between dots and matches multiple strings.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, this one works, (i.e. '/^{\$([a-z]*)\.([a-z]*)\.([a-z]*)}$/'), but it needs the 'i' modifier. (It fails to match uppercase letters.) Also, if you need to replace a bunch of these in a string, then you don't want to use the ^$anchors.
Thanks :) Actually it was like that at the beginning, I had simplified a bit for debugging.
1

This tested function has the (commented) regex you want:

function process_data(&$text)
{
    $re = '/# Reformat a string.
        \{       # Opening delimiter.
        (\$\w+)  # $1: game.
        \.       # Parts separated by dot.
        (\w+)    # $2: name.
        \.       # Parts separated by dot.
        (\w+)    # $3: fullname.
        \}       # Closing delimiter.
        /ix';
    $text = preg_replace($re, "$1['$2']['$3']", $text);
    return $text;
}

2 Comments

Nice, didn't know about the line breaks & comment support for regular expressions (apparently the 'x' flag)
@Michael Butler - Glad you discovered the "x" free-spacing mode modifier! Its good practice to write your regexes in this manner from the get-go. This also allows you to provide indentation for nested parentheses just like in other programming languages. (and it greatly eases any future maintenance as well.) Thanks for the comment.
0

If you are asking how to convert a string like "{mario.jumper.two}" to a php array like $game['mario']['jumper']='two'

$input="{mario.jumper.two}"
$input=str_replace( array('{','}') , '' ); //strip out brackets
$inputArray=explode('.',$input) // creates array( 'mario','jumper','two')
$game=array(
  $inputArray[0] => array(
      $inputArray[1] => $inputArray[2]
      )
);

Comments

0

If you are asking how to replace {$game.name.fullname} with the value of $game['name']['fullname'], my first suggestion would be not to allow the template to include any old variable (you don't want access to vars like $secret_password), but make an array of values that can be included.

Take a look at this:

<?php

$games = array(
    'game' => array(
        'name' => array(
            'fullname' => 'test'
        )
    )
);

$matches = array();
preg_match('/{\$([^.]+)\.([^.]+)\.([^}]+)}/', '{$game.name.fullname}', $matches);

var_dump($matches);

echo '<hr/>';

echo $matches[1] .'<br/>';
echo $matches[2] .'<br/>';
echo $matches[3] .'<br/>';

echo '<hr/>';

echo $games[$matches[1]][$matches[2]][$matches[3]];

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.