0

I have a PHP file that renders an HTML file, inside this HTML file I have this piece of code

<div class="app">{: echo $this->content :}</div>

And I want to replace the opening {: and closing :} tags with the traditional <?php ?> tags to make it look something like this:

<div class="app"><?php echo $this->content ?></div>
3
  • And so what have you tried? Commented Mar 3, 2014 at 20:45
  • If there are any collisions with text being :}/{: then you could use something like str_replace(...). Commented Mar 3, 2014 at 20:46
  • load html to string, and do php.net/str_replace Commented Mar 3, 2014 at 20:47

3 Answers 3

4
$contents = file_get_contents ("file.php");
$contents = str_replace(array('{:', ':}'), array('<?php', '?>'), $contents);
file_put_contents("file.php", $contents);
Sign up to request clarification or add additional context in comments.

Comments

0

You could do a simple string replace if you are doing this within PHP

str_replace(array('{:', ':}'), array('<?php', '?>'), $file_content)

Comments

0

Try this :

$string = '<div class="app">{: echo $this->content :}</div>';
$string = str_replace('{:','<?php',$string);
$string = str_replace(':}','?>',$string);

If you want to catch the content inside the you can use this :

$string = '<div class="app">{: echo $this->content :}</div>';
preg_match('/<div class="app">(.+)<\/div>/',$string,$preg_array);
$string = str_replace('{:','<?php',$preg_array[1]);
$string = str_replace(':}','?>',$string);

Output :

<?php echo $this->content ?>

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.