Jump to content

Recommended Posts

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 by jyeager11

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 by requinix
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.