在某些情况下更改HTML文件[关闭]

I have various html files in which I need to fill 3 Code snippets at 3 different positions. once directly after head starts, once in beginning and once in the end of the body.

I know where those html files are, cause I have a database for some other reasons, where I can also track down the html-files.

So I can run my php, getting all the files from database, then I need to check if my codesnippets are already existing in each file, if they do I do nothing, if not I need to add the snippets.

Even so php might not be the best choice, I do not have the time for now to learn something new (even though I appreciate tips, with which language this would work best for later)

I checked similar posts, but I can´t find the answer i´m seeking for

First, it's helpful to distinguish between a "file" and a "filename." When you say that you will be "getting all the files from database," I assume you mean that you'll be getting the filenames from the database.

If so, you can use the PHP "file_get_contents" function to read the file contents into a string. Then you can search the string (using various string functions such as "strpos" or "stripos") to see if it contains the HTML snippet you're looking for.

If the HTML snipped you're looking for isn't there, you can modify the string (again using various string functions) to add it. Search the PHP documentation for "string functions" since there are different ways to go about doing this.

When you're done you can write the altered string back to file using the "file_put_contents" function.

I won't go into the function syntax since you will, of course, want to research that in the PHP documentation regardless of what you read here.

There may be other problems with file permissions. If you get errors that you can't write to a file, you'll need to look into the chmod function.