1
I intend to create a php function that creates html elements in a more agile way, the syntax should be more or less like a sublime extension does:
create("p#paragrafo.classe1.classe2.classe3=content");
a saida devera ser: <p id="paragrafo" class="classe1 classe2 classe3">content</p>
Only if I pass like this p#.classe1=content
he will create <p id="" class="classe1">content</p>
. You got a problem with that id=""
?
The finalized function:
function create ($str) {
$text = substr($str, strpos($str, "=") + 1);
$close = "";
$str = str_replace("=$text", "", $str);
for ($i = 0, $j = substr_count($str, ">") + 1; $i < $j; $i++) {
# substring
$substr = ($pos = strpos($str, ">"))?substr($str, 0, $pos):$str;
# posicao id
$posId = strpos($substr, "#");
# posicao class
$posClass = strpos($substr, ".");
# tag
$tag = substr($substr, 0, $posId);
# id
$id = substr($substr, $posId + 1, $posClass - $posId - 1);
# class
$class = str_replace(".", " ", substr($substr, $posClass + 1));
# nao sei o que comentar
$close = "/$tag$close";
$str = str_replace("$substr>", "", $str);
# saida
echo "$tag class='$class' id='$id'";
echo "<br><br>";
}
# saida
echo $text;
echo "<br><br>";
echo $close;
}
Your question is: All HTML elements must contain an ID?
– Marconi
not only if I can set id but not write anything between quotes, id=", it has some problem?
– Júlio Souza Pereira
Problem, no, it’s just unnecessary and it doesn’t make much sense to do it. By specifying
p#paragrafo
should not be generated<p id="paragrafo">
? And why not specifyp.classe1
only, if theid
is not necessary?– Woss
Is an ID allowed to be Empty in an HTML tag?
– Marconi
id="" is the same as not defining an id or will identify as if it had an id with an invalid name?
– Júlio Souza Pereira
Try validating here https://validator.w3.org
– vinibrsl
You can preferably detect empty elements and remove them or add them.
– Guilherme Nascimento
any serious problems in function? Or is it possible to use?
– Júlio Souza Pereira