Do you have a problem creating an element and setting the id to empty?

Asked

Viewed 49 times

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;
}
  • 1

    Your question is: All HTML elements must contain an ID?

  • not only if I can set id but not write anything between quotes, id=", it has some problem?

  • 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 specify p.classe1 only, if the id is not necessary?

  • id="" is the same as not defining an id or will identify as if it had an id with an invalid name?

  • Try validating here https://validator.w3.org

  • You can preferably detect empty elements and remove them or add them.

  • any serious problems in function? Or is it possible to use?

Show 3 more comments

2 answers

0

In accordance with the W3 (translation of mine):

ID and NAME tokens must start with a letter([A-Za-z]) and can be followed by any number of letters, digits ([0-9]), hiphens("-"), underscores("_"), colons(":") and endpoints(".").

According to this definition empty id’s are not valid. Although nothing prevents you from using them it is not possible to guarantee how the browsers will black out.

0

No problem, in case you don’t need to reference the element ID in your project.

Regarding the reply of the friend @Genos, although he cited the source of W3, the ID and NAME not necessarily need to start with lyrics.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.