Jump to content

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


jyeager11
Go to solution Solved by Ch0cu3r,

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

Link to comment
Share on other sites

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);
Edited by Psycho
Link to comment
Share on other sites

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".

Edited by jyeager11
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.