Replace strings with existing ones

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dash
Forum Newbie
Posts: 5
Joined: Sun Jun 13, 2004 5:27 pm

Replace strings with existing ones

Post 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
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post 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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

include 'htmlfilename.html';
at the end of the script.. maybe.
Dash
Forum Newbie
Posts: 5
Joined: Sun Jun 13, 2004 5:27 pm

Post 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 ^_^
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

That presumes that .html files are parsed as php files, which they arn't by default :o
Dash
Forum Newbie
Posts: 5
Joined: Sun Jun 13, 2004 5:27 pm

Post 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
Dash
Forum Newbie
Posts: 5
Joined: Sun Jun 13, 2004 5:27 pm

Post 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?
Last edited by Dash on Sun Jun 13, 2004 6:10 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
Post Reply