A little about myself, I've been creating websites for about 6 years now and finally have gotten around to learning PHP. Coming from a background of mostly mud object oriented code and javascript I'm finding the learning process easy enough. My only problem is that I need a few examples of each particular function in use before I have a good enough grasp on it to use it to it's full extent.
In that, the tutorials I have found have been excellent for giving me a good overall picture of how to write in php and I find cranking out dynamic pages quite fun. However, there are from what I can tell some less common uses of PHP that are not as well documented in tutorials or even in the PHP manual's description of individual functions.
My particular problem at the moment is that I am trying to build a php script that will when executed with the correct password and input, take a html page and update a particular part of it's content. (eg: switch one image with another, add an image, add a link, add new text etc.) The interface on this script will be quite user friendly so that someone who knows practically nothing of html can update the content of a page easily.
For the most part this script is easy enough to design and in fact I have it at least 50% complete. The problem that is holding me up however is this. I need a way to get the posted data from the user side of the script into the html file. To that end I have come up with three different ways to do just that.
Option (1)
I put commented out unique tags into the html files to determine the beginning and end of a particular piece of content. I then with the script read the html file chosen by the user into a string that I then search for the beginning and ending tags with whatever might be between the two probably represented by a wildcard character. I replace that and only that part of the string with the input I got from the user enclosed in two tags identical to the ones I just found and then open the html file for writing and write the string to the file, closing it when done.
Option (2)
Same as above, but instead of using tags I keep the particular bit of content I want to be able to replace on a certain line in the html file I then replace that line and only that line with the user's input and save the file. This is a method I would rather avoid as it is more complex and potentially more unstable with regards to updates to the html files.
Option (3)
I create three different files for each html page I want on the site. The first is the actual page that will be the end result, the second the template design for that particular page and the third a file containing the content of each content section of that page. I read the template which contains variables for content into a string, read the content data from the not updated parts of the page into appropriate variables and then read the user's input into the variable that will be updated. I then write the whole thing to a html file that will be viewable and update the content file to include the newly updated input.
Any of these options would work but this is where I am having trouble. It is hard finding information on certain functions that I need to implement any of my ideas. I can get data from forms to the variables, I can read the html file into a string, I can save that string to a file. But what I can't do is replace only a very specific part of the string that could be of any length and is enclosed by tags. I also don't know how to edit only a specific line of a string if there is such a thing once a string is created. And I don't know effectively how to format a file so that I can put data into it and then retrieve each individual piece of data as a variable. I don't know the functions or there use. I have been experimenting with str_replace and preg_replace for the string replacing but I have yet to find success and I am not sure where to start with the data file. (Note I would really like to avoid the need to install and use mysql or another database filing system.)
If anyone could help me by outlining the functions I need and giving me a few basic examples of their use in the conditions I am describing I would be quite grateful as it will save me perhaps another week of searching for the answers I need.
Thanks in advance. - Naito