Pepijn Gieles
Pepijn Gieles

Reputation: 727

Replace HTML elements content of a PHP/HTML file with PHP

Problem

I'm trying to edit HTML/PHP files server side with PHP. With AJAX Post I send three different values to the server:

The PHP file I have now looks like this:

<?php
    $data = json_decode(stripslashes($_POST['data']));
    $count = 0;
    foreach ($data as $i => $array) {
        if (!is_array($array) && $count == 0){
            $count = 1;
            // $array = file url
        }
        elseif (is_array($array)) {
            foreach($array as $i => $content){
                // $array[0] = id's
                // $array[1] = contents
            }
        }
    }
?>

As you can see I wrapped the variables in an array so it's possible to edit multiple elements at a time. I've been looking for a solution for hours but can't make up my mind and tell what's the best/possible solution.

Solution

I tried creating a new DOMElement and load in the html, but when dealing with a PHP file, this solution isn't possible since it can't save php files:

$html = new DOMDocument(); 
$html->loadHTMLFile('file.php'); 
$html->getElementById('myId')->nodeValue = 'New value';
$html->saveHTMLFile("foo.html");

(From this answer)

Opening a file, writing in it and saving it comes is another way to do this. But I guess I must be using str_replace or preg_replace this way.

$fname = "demo.txt";
$fhandle = fopen($fname,"r");
$content = fread($fhandle,filesize($fname));

$content = str_replace("oldword", "newword", $content);

$fhandle = fopen($fname,"w");
fwrite($fhandle,$content);
fclose($fhandle);

(From this page)

I read everywhere that str_replace and preg_replace are risky 'caus I'm trying to edit all kinds of DOM elements, and not a specific string/element. I guess the code below comes close to what I'm trying to achieve but I can't really trust it..

$replace_with = 'id="myID">' . $replacement_content . '</';
if ($updated = preg_replace('#id="myID">.*?</#Umsi', $replace_with, $file)) {   
    // write the contents of $file back to index.php, and then refresh the page.
    file_put_contents('file.php', $updated);
}

(From this answer)

Question

In short: what is the best solution, or is it even possible to edit HTML elements content in different file types with only an id provided?

Wished steps:

Upvotes: 1

Views: 5341

Answers (1)

Spork
Spork

Reputation: 1651

First of all, you are right in not wanting to use a regex function for HTML parsing. See the answer here.

I'm going to answer this question under the presumption you are committed to the idea of retrieving PHP files server-side before they are interpreted. There is an issue with your approach right now, since you seem to be under the impression that you can retrieve the source PHP file by the URL parameter - but that's the location of the result (interpreted PHP). So be careful your structure does what you want.

I am under the assumption that the PHP files are structured like this:

<?php include_some_header(); ?>
    <tag>...</tag>
    <!-- some HTML -->
<?php //some code ?>
    <tag>...</tag>
    <!-- some more HTML -->
<?php //some code ?>

Your problem now is that you cannot use an HTML reader (and writer), since your file is not HTML. My answer is that you should restructure your code, separating templating language from business logic. Get started with some templating language. Afterwards, you'll be able to open the template, without the code, and write back the template using a DOM parser and writer.

Your only alternative in your current setup is to use the replace function as you have found in this answer. It's ugly. It might break. But it's definitely not impossible. Make backups before writing over your own code with your own code.

Upvotes: 1

Related Questions