1

I am trying to find a way to search through a page in php to replace the names of form elements.

I guess I should explain. I'm doing a job for a friend and I want to make an easy database updater that is robust and can withstand adding elements without the person knowing much about php or databases.

In short, I want to search through a form and replace all the name="%name%" with the respective database table key names, so I can use a simple foreach method to update the table.

So I was looking at the DOMDocument element to open an html page and replace every form name inside in order with the corresponding table keys, but I wasn't sure if I can open a php page with loadHTMLfile or not. And, if I could open up a php page, would opening itself cause an infinite loop? Or would it just parse the html as if it were looking at client-side html?

Is there any way to do what I want? If not, that's OK, I'll just make it a little less awesome, but I was just wondering.

2
  • What's wrong with a project level search and replace in your IDE? Commented Apr 23, 2011 at 19:29
  • Well, that's fine, but it's not at all what I'm trying to achieve... I want someone to be able to come in who doesn't know what the heck they're doing to be able to add a form element and set the name to %name% and then the php script would automatically replace the %name% value with the appropriate key name from the database so that it would be able to translate the name easily in my foreach table updater so that it would be robust for the future. So, I want a server-level solution, not a developer-level. I'm not going to be able to update the site all the time. Nor do I want to. Commented Apr 23, 2011 at 19:33

1 Answer 1

3

It's perfectly doable.

The DOMDocument is possibly the ideal (native) tool for this task, but you'll probably want to look into the DOMDocument::loadHTML() method instead of the loadHTMLfile() one.

To get the processed PHP page into a string, you can request the page with CURL, file_get_contents() or a similar alternative. This involves making an additional request and adding specific control logic to avoid an endless loop.

A better alternative might be to use output buffering, here is a simple example I have at hand in how to replace the contents of the <title> tag:

<?php

ob_start();

echo '<title>Original Title</title>';

/*    get and delete current buffer      &&  start a new buffer */
if ((($html = ob_get_clean()) !== false) && (ob_start() === true))
{
    echo preg_replace('~<title>([^<]*)</title>~i', '<title>NEW TITLE</title>', $html, 1);
}

?>

I am using preg_replace(), but you shouldn't have any problems adapting it to use DOMDocument nodes. It's also worth noticing that the ob_start() call must be present before any headers / contents are sent to the browser, this includes session cookies and so on.

This should get you going, let me know if you need any more help.


A generic DOMDocument example:

<?php

ob_start(); // This must be the very first thing.

echo '<html>'; // Start of HTML.
echo '...'; // Your inputs and so on.
echo '</html>'; // End of HTML.

// Final processing, the $html variable will hold all output so far.
if ((($html = ob_get_clean()) !== false) && (ob_start() === true))
{
    $dom = new DOMDocument();

    $dom->loadHTML($html); // load the output HTML

    /* your specific search and replace logic goes here */

    echo $doc->saveHTML(); // output the replaced HTML
}

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

3 Comments

Wow thanks for the great response. I was looking into the DOMDocument::loadhtml() method and the savehtml method, and the removechild and addchild methods as well. I just wasn't sure how to avoid the infinite loop that I realized would probably happen. could you point me in the right direction to avoid that? Thanks a ton!
@Ryan: If you use output buffering you don't need to make a new request and thus the infinite loop never happens, I'll post a generic example adapted to DOMDocument in a sec.
Yeah I just went and read the ob documentation that you linked here. It is a WAY easier solution that I was considering!!

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.