preg_replace

Hi guys

when using $content = file_get_contents(‘form.php’); and preg_replace together they work fine

however i wanted to add some php in the form.php file and it doesnt pick it up unless i change this

$content = file_get_contents(‘form.php’);

to this

$content = include(‘form.php’);

the php now works in the file but the preg_replace has messed up and the formatting of the form :frowning:

is there a way i can use php in a file and also preg_replace?

thanks

You can’t do:


$content = include("form.php");

Include includes and evaluates the file. It doesn’t return anything (it’s a statement, not a function).

What problem are you trying to solve? There probably is another, better way.

basically i have a template file that is calling form.php

it is currently using $content = file_get_contents(‘form’.php)

form.php only contain html at the moment, i want to add php code to it, The php code is not being picked up

if i change it to $content = include(‘form’.php)

the php is picked up but the formatting breaks :shifty:

anyone??

Don’t fall between two stools.
Use only one technique: preg replace or PHP.
Do not mix it.

so if i got rid of the preg replace and used echo $variable in the form.php would that be right?

yes.
It is called “Native php template system” and widely used nowadays

Actually. You can do that.
If the file you include has “return” outside of a function block.
I’ve used that method to bring in an array I exported out with var_dump many of times.

You’re right! I didn’t know that. The point still holds, though: it doesn’t return the included file’s output. It returns, well, whatever you want it to return (which can be the output of the included file with a little help of output buffering).

But OP were asking if it can return something different though :slight_smile:
It can, and you can even demonstrate it to show your knowledge.
But the answer remain the same - better not to mix technologies.

I agree. I always use PHP as a template engine. PHP is a template engine. There is no point adding parsing (performance) and readability overhead.