Okay, so my assignment is I need to read an external HTML file using PHP. Now the tricky part is I need to use PHP variables inside the HTML file. So it needs to read line by line and then replace/add a PHP variable that displays the information. Currently, this is what I have for reading the HTML file line by line into a string. $order = fopen($orderFile, 'r'); while (!feof($order)) { $data = fgets($order, 512); echo $data; } fclose($order); Code (markup): Now however I am having a problem replacing items brought in from the HTML with the PHP Variable. Now this is part of the HTML code I am reading in... <head> <title>Bob's Auto Parts :: Order Results</title> </head> <body> <p> <h1>Bobs Auto Parts</h1> <h2>Order Results</h2> Referred by: $strFindText </p> Code (markup): Now since this a HTML file $strFindText won't work... I need some way in the PHP to replace $strFindText with the PHP Variable to output the stored information. Thanks ahead of time for the help.
It sounds like you're looking for str_replace. By the way, you can get the file contents in a single line with $data = file_get_contents($orderfile);
First I don't see why you need to load file and then replace variable placeholders with variable values string by string. this is example that will do the job (you can put this code in same file): <?php $tmpVar = 123; ?> <html> <head><title>Test</title></head> <body> Variable tmpVar: <?php echo $tmpVar; ?><br /> or tmpVar: <?=$tmpVar?> </body> </html> Code (markup): or you can put html in separate file with php extension and do something like this <?php $tmpVar = 123; require('pathToFile.php') ?> Code (markup):
Well I have multiple variables that I wanted to change and I didn't think that if I read it in as one big string that I would be able to change various variables. I do have another problem later on in my code... Currently I have. $form = file_get_contents($formFile); if ((!isset ($btnsubmit)) || ($blnEditPass == False)) : echo $form; if (isset ($btnsubmit) && ($blnEditPass == False)) : $error = str_replace ("inverror","diserror", $form); echo $error; endif; endif; Code (markup): The problem is I want to echo out the form to the user. Then I want to verify there data and if there is anything wrong with it use string replace to display the error message and then show out the form with the error message. The problem I am having is it echos the form out correctly then when it verifies the data and it is wrong.. creates a new form + error message right underneath the first one. Is there anyway to get rid of the first form and display the one with the error message? Again thanks ahead of time for the help.