0

How can I change

{for ...cond.... }
somethinginbetween
{/for}

to

<?php for (...cond...){ ?>
somethinginbetween
<?php } ?>
6
  • possible duplicate of How do I replace certain tags with the content of PHP variables? Commented Apr 2, 2011 at 10:20
  • Is is specifically for tags, or could it any tag using that language Commented Apr 2, 2011 at 10:20
  • it could be using if, else if, elseif, foreach, while, do while, nothing else Commented Apr 2, 2011 at 10:23
  • what do you need that for? are you going to translate a smarty template into a php file? if so i'd actually do it by hand. you also could use the alternative versions of php tags like this: instead of for(){....} you write: for(): .... endfor; with that you can replace each {for cond} with <?php for(cond):?> and each /for with <?php endfor: ?> the same is applicable to if, foreach, switch ..... see here php.net/manual/de/control-structures.alternative-syntax.php Commented Apr 2, 2011 at 10:24
  • I have a feeling that this could be achieved with two pregmatch statements in a loop, for example get each set of {for cond}{/for} then replace that to the php code and loop through all the items. However I am extremely poor skilled with regex and I need this code really now to be honesh. Thanks. Commented Apr 2, 2011 at 10:25

1 Answer 1

1

This is a quick-and-dirty string literal string replacement:

$code = str_replace('{for ', '<?php for (', $code);
$code = str_replace(' }', '){ ?>', $code);
$code = str_replace('{/for}', '<?php } ?>', $code);

It will not work if somethhingbetween contains if(){ } because } gets replaced by ){ ?>. But for other cases with just {for condition } and {/for}, it suffice.

Regular expression approach combined with simple string replacement, this assumes no occurences of } in the for loop (match everything except for }: [^}]+)

$code = preg_replace('/{for ([^}]+)}/', '<?php for ($1) { ?>', $code);
$code = str_replace('{/for}', '<?php } ?>', $code);

This function does no validating whatsoever, it's your responsibility to feed it with properly opened / closed {for} and {/for}s.

If you need to execute PHP, insert PHP. The other way would be writing your own parsing code. You could look at template projects like Smarty.

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

3 Comments

this replace all the } with {?> but i dont wanna do that.
@user658911: added regex approach. Note that for != foreach. So you'll have to use something like {for $i=0; $i<2; $i++}...some code...{/for}.
if you use the alternative php syntax then you could do the replacement like this: {for with <?php for( then replace all {/for} with <?php endfor;?> and in the end any remaining } with ):?>

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.