I am currently working on an edit page on a website that gets the content from an HTML or PHP file and inserts it into a 'fill in the blank' (not sure what to call this) page where I can edit the content and POST it, overwriting the old file with new edited data. The data I am trying to get includes a table (like the one below) as well as an unordered list and some other random text. I have been looking around for ways to do this with file_get_contents as well as fopen(...), finding the data and inserting it into the proper html input editing spaces. This site stores the data only in the files not in a db. Does anyone have a suggestion as to what I could do in this situation?
Table Example
<table>
<tr><th>Ingredient</th><th>Amount</th></tr>
<tr><td>Example</td><td>Example</td></tr>
</table>
An example of the file_get_contents and something I found here which just gets a bit of text from the html file. I tried with table data but just displays it as one big string.
<?php
$fileToEditPath = $_POST['editPath']; //editPath is the full path to the file
$fileEdits = file_get_contents($fileToEditPath);
$doc = new DOMDocument();
$doc->loadHTML($fileEdits);
$divs = $doc->getElementsByTagName('div');
foreach($divs as $div) {
if ($div->getAttribute('id') === 'recipe-information') {
echo $div->nodeValue;
//Alternatively if this ends up working, echo into something more like this to POST
echo "<input type='text' name='specificinstructions[]' value='" . $div->nodeValue . "'/>";
}
}
?>
I do not have any ideas as to what I should try after many hours over several days looking for solutions. I apologies if this is very simple, I am quite new to working with PHP. For a tiny bit more context here is a link to another question outlining more of what I am creating and how the file I am getting data from is created.
Just as I am posting this seeing suggested tags, would this be an easier task to do with javascript/jquery? I am going to investigate that now, but any suggestions would be appreciated.
question from:https://stackoverflow.com/questions/65545494/file-get-contents-find-and-edit-data