Jump to content

(Beginner) Defining a string in a separate file...


jyeager11

Recommended Posts

$poststart = "<!-- Begin -->";

$poststop = "<!-- End -->";

 

$parsedpost1 = explode($poststart, $contenu);

$parsedpost2 = explode($poststop, $parsedpost1[1]);

$contenu = $parsedpost1[0] . $poststart . $newpost . $poststop . $parsedpost2[1];

 

The above code will set 2 markers and replace whatever inside by $newpost, which is defined elsewhere in the page. But what if $newpost is so long that I'd be more comfortable storing it in a separate file? 

 

How would I go about calling said file on that last line? (I'm referring to the specific syntax.)

 

Thx

Well, the current code can be improved. There is no reason to explode the source text. Just use string functions for this. But, to get the contents of an external file you would use the function file_get_contents()

 

I would put the process into a function to make it more extendable

function replaceContents($inputString, $startTag, $endTag, $replaceString)
{
    //Determine positions for replacement
    $startPos = strpos($inputString, $startTag);
    $stopPos =  strpos($inputString, $endTag) + strlen($endTag);
    $length = $stopPos - $startPos;
    //Make replacement and return result
    $output = substr_replace($inputString, $replaceString, $startPos, $length) . "\n";
    return $output;
}
 
$poststart = "<!-- Begin -->";
$poststop = "<!-- End -->";
$replaceString = file_get_contents("replace.txt");
 
$contenu = replaceContents($contenu, $poststart, $poststop, $replaceString);

How difficult would it be to modify the original code posted (not the updated version) so that the markers are INCLUDED in the text being replaced? Currently, they are not -- only the content between them is.

 

In other words, here's your first marker. Here's your second. Replace the markers and everything in-between by "this".

How difficult would it be to modify the original code posted (not the updated version) so that the markers are INCLUDED in the text being replaced? Currently, they are not -- only the content between them is.

 

In other words, here's your first marker. Here's your second. Replace the markers and everything in-between by "this".

 

Why modify the original code that uses a less optimal process? THe code I provided does just what you ask. At least it did in the limited testing I did.

To be honest, the plan was the use the "unpolished" code just to get the page to where I want it – and then look at optimizing it the way you just did – because I don't know what 100% of the needs are yet. Like a pencil outline before inking an illustration.

Then, once I know what all my dynamic-text-replacement needs are, I can 1) Optimize the PHP file through smarter function building, and 2) Use the final updated code as a model for all future tasks of this kind.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.