Replace strings with existing ones
Moderator: General Moderators
Replace strings with existing ones
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
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
- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
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:
That's the general idea - of course, you can get as fancy as you like!
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;- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
include 'htmlfilename.html';Wouldn't that juste add the text and not make the strings change into their proper values?feyd wrote:at the end of the script.. maybe.Code: Select all
include 'htmlfilename.html';
Thanks for the help by the way, I'm trying out all your suggestions ^_^
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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
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
I just finished trying this one, it works like a charm. Thanks!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:
That's the general idea - of course, you can get as fancy as you like!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;
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.