Self modifying PHP script
I'm trying to modify a part of a PHP script structured like this barebone example
<-- part A -->
function modify_B($string)
{
some code to mod开发者_Go百科ify part B
}
<-- end A -->
<-- part B -->
<container>some XML</container>
<-- end B -->
<-- part C -->
<-- end C -->
I'd like to modify part B without changing the rest of the file, because A and B are the logic of the script which should not change.
Could somebody help me? Thank you in advance for your help.
It looks like, from your example, it's just some string data XML. So load the content into a string somehow (either set a variable with standard string notation, or read it from the contents of a separate file), modify the string according to your whims, and then echo the string to the output. Then it's not a problem of being self-modifying anymore. It's just a matter of being data-driven.
Do you mean that the XML is outside of your PHP <? ?>
script tags? So you want to modify the text that's about to be output by the PHP script?
If that's the case, remember that anything outside of script tags is just treated as a string, which PHP outputs as if you had written echo $string;
. So just save your changed data in a string variable, and echo it.
Or if you need persistence in the changes, put "B" in a file and include or read it.
You should never write self modifying code (unless you are writing assembly) it can cause all sorts of problems, consider for example what happens if there is a bug that destroys the code in the file.
Split your data out from the code and load it with a require_once command
You can then use standard file reading and writing commands to edit the data http://www.php.net/manual/en/book.filesystem.php
... or better still since the data is XML, save the file as an XML file and use simple xml to maintain the data http://php.net/manual/en/book.simplexml.php
精彩评论