Page 1 of 1

Replace strings with existing ones

Posted: Sun Jun 13, 2004 5:27 pm
by Dash
Alright, I'm a bit of a beginner in PHP but I'm learning pretty fast.

My problem is that:

I have a php file that includes a series of strings such as $name, $email, $score, etc. and a second one, a HTML page, which includes a website design with $name, $email, $score written all over it.

What I want to do is that every time i run the php file, it will go through the HTML page and detect every "$name" "$email" and "$score" text and replace it with the PHP file's strings of the same name... Meaning that if $name = "John" in the php file, the $name in the HTML file would be replaced by John. (Of course, the HTML file would be modified by the PHP script before being shown)

I have tried fgets and the like. I also tried to put the entire html on a single string and mangle with it but I have no clue how to make every $string from the HTML to match the ones from the php file where the HTML is opened and fgets'ed.

I'm also trying to avoid making a fopen and a fwrite ($code) with $code being tons of /" and /> if you know what I mean :S

Thanks for any help :S

Posted: Sun Jun 13, 2004 5:34 pm
by launchcode
What you need is a template system - there are lots of them out there, but you can easily write a basic one yourself. The following should do the trick:

In your HTML file, instead of having the actual variable (i.e. $name) replace it with something like {name} instead. Then use code like this:

Code: Select all

$tags = array('{name}','{age}','{email}');
$values = array($name, $age, $email);

$html = file_get_contents('page.html');

$html = str_replace($tags, $values, $html);

echo $html;
That's the general idea - of course, you can get as fancy as you like!

Posted: Sun Jun 13, 2004 5:40 pm
by feyd

Code: Select all

include 'htmlfilename.html';
at the end of the script.. maybe.

Posted: Sun Jun 13, 2004 5:44 pm
by Dash
feyd wrote:

Code: Select all

include 'htmlfilename.html';
at the end of the script.. maybe.
Wouldn't that juste add the text and not make the strings change into their proper values?

Thanks for the help by the way, I'm trying out all your suggestions ^_^

Posted: Sun Jun 13, 2004 5:50 pm
by John Cartwright
If you declare the variables name

ie. $name = "john";

and on the page which contained the html you had <?=$name?>

it would print John

Posted: Sun Jun 13, 2004 5:53 pm
by markl999
That presumes that .html files are parsed as php files, which they arn't by default :o

Posted: Sun Jun 13, 2004 5:58 pm
by Dash
I just wanted to precise this:

A form sends string information to the PHP File, within the PHP file, it takes the form's information and associates each of them with a specific string. Then, the same PHP file creates a new html file made from a 'layout' html file that includes the $strings. These strings are supposed to be replaced by the PHP file's form submitted strings and then the whole thing is saved in the newly created html file.

I don't mind changing the html files to php though, but that won't change much to the script (if you read the above) because i could also make the 'layout' html as a txt file

Posted: Sun Jun 13, 2004 6:09 pm
by Dash
launchcode wrote:What you need is a template system - there are lots of them out there, but you can easily write a basic one yourself. The following should do the trick:

In your HTML file, instead of having the actual variable (i.e. $name) replace it with something like {name} instead. Then use code like this:

Code: Select all

$tags = array('{name}','{age}','{email}');
$values = array($name, $age, $email);

$html = file_get_contents('page.html');

$html = str_replace($tags, $values, $html);

echo $html;
That's the general idea - of course, you can get as fancy as you like!
I just finished trying this one, it works like a charm. Thanks! :D

I have the same data in a mysql database. I made the phpfile write to a mysql using a column for every single string that recieved information through the form too. Should it be better retrieving from the mysql?

Posted: Sun Jun 13, 2004 6:09 pm
by feyd
it's possible to do with preg_replace_callback or some special footwork with preg_replace.. however.. the data would need to be global, or accessible from a superglobal..