1

I am trying to add foreach statements with preg_replace but it keeps throwing the error preg_replace(): Parameter mismatch, pattern is a string while replacement is an array Because of the foreach that deals with the vars it wont allow me to place arrays. How can I make it use both?

here are the class

private $vars = array();

public function assign($key, $value) {
    $this->vars[$key] = $value;
}

public function render($file_name) {
    $path = $file_name . '.html';

    if (file_exists($path)) {

        $content = file_get_contents($path);

        foreach ($this->vars as $key => $value) {
            $content = preg_replace('/\{' . $key . '\}/', $value, $content);
        }

        /* PHP Tags */
        $content = preg_replace('/\{php\}/', '<?php ', $content);
        $content = preg_replace('/\{\/php\}/', ' ?>', $content);

        /* IF Statement */
        $content = preg_replace('/\{if (.*)\}/U', '<?php if ($1): ?>', $content);
        $content = preg_replace('/\{elseif (.*)\}/U', '<?php elseif ($1): ?>', $content);
        $content = preg_replace('/\{else\}/U', '<?php else: ?>', $content);
        $content = preg_replace('/\{\/if\}/U', '<?php endif; ?>', $content);

        /* FOREACH Statement */
        $content = preg_replace('/\{foreach (.*)\}/e', '<?php foreach ($1) { ?>', $content);
        $content = preg_replace('/\{\/foreach\}/e', '<?php }; ?>', $content);

        eval(' ?>' . $content . '<?php ');

here is the variable file

include_once 'gate.php';

$gate = new Gate;

$gate->assign('pagetitle', 'Test Document');
$gate->assign('username', 'Zach');
$gate->assign('age', 21);

$names = array(
        "Name",
        "Name2",
        "Name3",
        "Name4"
    );

$gate->assign('members', $names);

$gate->render('test');

and here is the HTML

{foreach ({members} as $member)}
       <p>$member</p>
{/foreach}

I just need to know how to fix the error which i assume involves making the array become accepted by the preg_replace function

11
  • 2
    I don't really have an idea what you're trying to do here. Commented May 10, 2013 at 22:59
  • @bažmegakapa I am trying to get the error to go away Commented May 10, 2013 at 23:00
  • More simply put i just need the preg_replace to accept the array. Does anyone know Commented May 10, 2013 at 23:04
  • This is not your real code (proof). Commented May 10, 2013 at 23:05
  • 4
    @zachstarnes: I put it there. It's a copy/paste of what you posted above. It does not produce the error you mention. Conclusion: you have not shown us the code that produces the error. Commented May 10, 2013 at 23:14

1 Answer 1

1

Set $names to a string containing the PHP code to create a literal array:

$names = 'array(
        "Name",
        "Name2",
        "Name3",
        "Name4"
    )';

$gate->assign('members', $names);
Sign up to request clarification or add additional context in comments.

8 Comments

If these names are ever meant to be dynamic, be very careful to have them escaped. Or build in some sort of array-unpacking functionality.
@Barmar thanks for the help the error is no longer there. Will this loop through just like a normal array?
It will if the output of your template code is evaluated by the PHP processor again. That seems to be what you expect with your foreach loop.
Nothing in the code you showed does that. It looks like it's taking a template file and writing a PHP file. I assume some other piece of the system executes that PHP file.
@barmar if you dont see anything that does then thats a problem because thats all the code I have lol :(
|

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.