jyeager11 Posted June 9, 2014 Share Posted June 9, 2014 (edited) Hi guys. First-timer here, please be gentle. =) I have 2 files on my server that are virtually identical, except for a script that's only located in one of them. I will soon have to create more similar files with only small changes in each. I'll spare you the long and boring explanation of why, but I now realize that it would be easier if I had a single file with all the code in it, and included that in all the other files. Only problem is, I can't modify included text for unique situations. Or can I? It has been brought to my attention that I can fetch the entire thing via file_get_contents(), and modify certain parts of it via str_replace(); so problem solved, right?$content = file_get_contents("code.htm");$content = str_replace("replace this line","with this line", $content); It works great for short strings, but what about long ones? What if I'm targeting a 100-line javascript function I want stricken on the fly, during the output? I mean I can't exactly just paste 100 lines of code between those quotation marks, can I?Could someone show me what the proper syntax to search/replace a large chunk of code on the fly would be? At first, I pictured something like this...$content = str_replace(" <script type=\"text/javascript\"> GS_googleEnableAllServices(); </script> ","", $content); ...and then realized I was straying too far from my limited PHP knowledge, because that looks wrong as hell. ;-) So here I am.Any help would be greatly appreciated. This one single hurdle has been keeping a project from advancing for a couple of days, now. Edited June 9, 2014 by jyeager11 Quote Link to comment https://forums.phpfreaks.com/topic/289090-modifying-html-non-destructively-via-php/ Share on other sites More sharing options...
requinix Posted June 9, 2014 Share Posted June 9, 2014 (edited) Make the file be PHP instead of straight HTML. All you have to do is change the extension to .php (and make sure there's no "<?php" PHP markup in it). Then you can start putting PHP code into it for the dynamic features, and the calling code can use include to execute it. include "code.php";If you want to vary bits of the content then use can use variables for it. Like in the calling code you have $show_gs = false; include "code.php";and in the code.php you have ... <?php if (!isset($show_gs) || $show_gs) { /* calling code didn't set $show_gs, or it did and set it to true */ ?> <script type="text/javascript"> GS_googleEnableAllServices(); </script> <?php } ?> ...There are more powerful methods depending on how your application is set up, though mostly to do with how this code.htm file works and is used. Edited June 9, 2014 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/289090-modifying-html-non-destructively-via-php/#findComment-1482345 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.