Search code examples
phpparsingdomdomdocument

How to get the starting HTML tag in pure PHP?


I need to get the starting <form> tag of an existing form, as is, with attributes, without content and the closing </form> tag, to use it for a new dynamically created form. So, I need only the string <form method="post" class="form" data-id="19" data-title="Schema" data-slug="schema" data-message-success="Success!" data-message-invalid-email="Not valid!" data-message-required-field-missing="All fields are required!" data-message-error="Error!"> How to do this in pure PHP?

My form:

<form method="post" class="form" data-id="19" data-title="Schema" data-slug="schema" data-message-success="Success!" data-message-invalid-email="Not valid!" data-message-required-field-missing="All fields are required!" data-message-error="Error!">

--- Form content ---

</form>

Right now I found only how to get the form attributes to add them to a new form. I know, I can just loop over that and and create the new form, but I am not an expert and I suppose that there can exist a more elegant solution.

function parseTag($content,$tg) {
    $dom = new DOMDocument('1.0', 'utf-8');
    $dom->loadHTML($content);
    $attr = array();
    foreach ($dom->getElementsByTagName($tg) as $tag) {
        foreach ($tag->attributes as $attribName => $attribNodeVal) {
            $attr[$attribName]=utf8_decode($tag->getAttribute($attribName));
        }
    }
    return $attr;
}

//$html variable contain the above form
$attrib_arr = parseTag($html,'form');
var_dump($attrib_arr);

Output:

array(9) {
  ["method"]=>
  string(4) "post"
  ["class"]=>
  string(19) "form"
  ["data-id"]=>
  string(2) "19"
  ["data-title"]=>
  string(23) "Schema"
  ["data-slug"]=>
  string(27) "schema"
  ["data-message-success"]=>
  string(50) "Success!"
  ["data-message-invalid-email"]=>
  string(52) "Not valid!"
  ["data-message-required-field-missing"]=>
  string(41) "All fields are required!"
  ["data-message-error"]=>
  string(25) "Error!"
}

Solution

  • As I understand your problem, you want the opening form tag with attributes. For this you could use something like the following:

    <?php
    declare(strict_types=1);
    
    $html = <<<HTML
    <form method="post" class="form" data-id="19" data-title="Schema" data-slug="schema" data-message-success="Success!" data-message-invalid-email="Not valid!" data-message-required-field-missing="All fields are required!" data-message-error="Error!">
        --- Form content ---
        <div>with some tags</div>
    </form>
    HTML;
    
    $doc = new DOMDocument('1.0', 'UTF-8');
    $doc->loadHTML($html);
    
    $xpath = new DOMXPath($doc);
    
    foreach ($xpath->query('//form') as $formTag) {
        // remove child nodes of the form tag
        foreach ([...$formTag->childNodes] as $child) {
            $formTag->removeChild($child);
        }
    
        // output as string, but remove the closing tag
        echo substr($doc->saveHTML($formTag), 0, -7);
    }
    

    (Demo)