Creating PHP object from HTML

8

I imagine that frameworks usually do the following to create a simple text field:

$campoTexto = new CampoTexto("nomeDoCampo", "valorDoCampo");
$campoTexto->gera();

The result would be:

<input type="text" name="nomeDoCampo" value="valorDoCampo">

My question is, is there any class / function that reverse-engineers the code above? I imagine something like this:

$tag = new ObjetoMilagroso("<input type=\"text\" name=\"nomeDoCampo\" value=\"valorDoCampo\">");
print $tag->type . " - " . $tag->name . " - " .$tag->value;

The result:

  

input - fieldName - field value

    
asked by anonymous 26.06.2014 / 21:18

1 answer

4

With the Fernando's suggestion I arrived at following conclusion:

<?php
    $t = "<input type=\"text\" name=\"nomeDoCampo\" value=\"valorDoCampo\" peterson=\"bonito\" />";
    $html = simplexml_load_string(html_entity_decode($t));
    $arrAtributos = $html[0]->attributes();
    $nome = $html[0]->getName();
    print "Nome da tag: ".$nome."<br />";
    print "Atributos:<br />";
    foreach ($arrAtributos as $key => $value) {
        print $key." => ".$value."<br>";
    }

It's a simple hint yet and needs tweaking, like:

The tag must have a closing like in XML (again, Fernando's suggestion):

<input> =  erro
<input /> = ok
<button></button> = ok

I'm not dealing with errors.

But I found it interesting because it ended up being "generic" and solved my problem.

    
26.06.2014 / 22:56