I would like to check if a given string could be a valid HTML attribute. If this isn't the case, I will add that string with a prefix of data-
to the element. How would I go about this?
For example when the user wants to add a attribute, it passes it to the $attributes
array like this:
$attr = '';
foreach ( $attributes as $key => $value ) {
if (is_attr($key)) {
$attr .= $key . '="' . $value . '" ';
} else {
$attr .= 'data-' . $key . '="' . $value . '" ';
}
}
So this will finally be added to a form element like an input
or textarea
or something like that.
... how would the implementation of is_attr($key)
look like?
Update:
I was hoping I could create the attribute with the DomDocument()
class and then validate it to see if the attribute is officially supported. No luck so far.