jyeager11 Posted June 12, 2014 Share Posted June 12, 2014 $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 Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 12, 2014 Share Posted June 12, 2014 (edited) 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 June 12, 2014 by Psycho Quote Link to comment Share on other sites More sharing options...
jyeager11 Posted June 14, 2014 Author Share Posted June 14, 2014 (edited) 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 June 14, 2014 by jyeager11 Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 15, 2014 Share Posted June 15, 2014 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. Quote Link to comment Share on other sites More sharing options...
jyeager11 Posted June 15, 2014 Author Share Posted June 15, 2014 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. Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted June 15, 2014 Solution Share Posted June 15, 2014 But what if $newpost is so long that I'd be more comfortable storing it in a separate file? Then replace $newpost in your code with file_get_contents Quote Link to comment 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.