0

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

4
  • 1
    On first sight this sounds like a Bad Idea™. Can you elaborate what you want to achieve with this in the end, what you need to modify the XML for? There are probably better solutions for what you want to do than self-modifying scripts. Commented Dec 30, 2009 at 1:11
  • why can't part B sit in a separate file on its own? Commented Dec 30, 2009 at 1:49
  • Maybe there are better ways to achieve the task. My intent is to realize a tiny website which sits in ONE file. So the need is to use ONE file, no more then one! The content is dynamic: in fact the part A is a php script which behaves like a rudimental content editor. Commented Dec 30, 2009 at 9:15
  • The part B is buffered through ob_start() function. When in view-mode, the part B is rendered by the part A script, if in edit-mode the part B should be copied into a variable, then modified by the part A script and rewritten (to became persistent) to the file replacing the old part B of the file. Commented Dec 30, 2009 at 9:28

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

Or if you need persistence in the changes, put "B" in a file and include or read it.

Comments

0

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

3 Comments

I see from comments that you want to use a single file for your website, well you can do that, it going to be far more complicated and you will have to do a fair bit of bespoke coding to achieve it but all you are really doing to be doing is reading the whole file into a var and manipulating it before writing it back out. Whatever method you take you will be going against bast practise. EDIT: Oops, didnt mean to necro a question!!!
well I have some maybe not self modifying, but self generating code for a dynamic if that I then just exec (obviously the generation is under a huge load of ifs so nothing unexpected can go in there. for example say you have a list of dates and date ranges and you want to check whether a date fits in it it will be hard without knowing the list before runtime is going to be hard, if the only thing you know is that you have a comma sepeated list of dates and rages. I just determine "single or range" and write a condition statement and concate it via OR.
why have the file at all? just write the php code, load everything into memory (i mean the xml part), then work with that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.