Replace Tag by php code

Asked

Viewed 319 times

1

Hello, I wonder if there is a way to do via php what sublime text does with snippets. I need for example to replace a tag with a pre-formatted code. Type:

{{repete start}}
  <div class="noticia">Conteúdo</div><br>
{{repete end}}

<!-- isso deverá ser interpretado como abaixo após acessado via web -->
<?php foreach ($noticias as $noticia): ?>
    <?= $noticia['texto'] ?><br>
<?php endforeach ?>

<!-- que no output ficará -->
Conteúdo
Conteúdo
Conteúdo
Conteúdo
Conteúdo
(quantos existirem no banco)

Is it possible to do this using php? My idea is to facilitate the work of integrators who use a php admin panel that the company uses. Thank you very much.

  • It’s not very clear what you want to do. What exactly is the relationship between the 3 code snippets?

  • The first is the tag I want to "create", to say that that block will repeat itself. The second block is the PHP that he should run when processing this on the page and the third block is the output in html of what he presents of this query (taking into account that the database and select query of the table I’m already doing above that).

  • It’s not that simple. What would have the repete start ? How would he know it was the news table? And how would he know that the content inside the div was a campo of the table and referring to the repeat start ?

  • yes has how and even use in my Framework. I can give an idea of how to do.

1 answer

1


Example of how to do with PHP: Ex:

{{ variavel }} vira <?php echo $variavel; ?>

PHP code:

preg_replace("/({{([ a-zA-Z]*)}})/gm", "<?php echo \$${2}; ?>", $string);

Of course you will make a fopen to open a file and write the modifications of the view you want. Site to test this regex: https://regexr.com/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.