We have a huge database where users can create custom fields. Every UTF-8 character is allowed in their name. Until a few weeks ago, when they export their data in XML, only invalid characters that users had in their tables were slash /
and whitespace characters, and we replaced them with underscores.
Now I see that some users who need an export in XML are using in their field names *
, !
... So if their field name instead valid_name
is named for example invalid*name!
, this script will break.
Part of code used for defining tag name:
$doc = new DOMDocument();
$elementName = is_numeric($key) ? (string)$name : (string)$key;
$elementName = str_replace(array('/', ' '), '_', trim($elementName));
$node = $doc->createElement($elementName); // here I get error "invalid character name"
Sample of valid XML:
<?xml version="1.0"?>
<rows total="621" page="1">
<row>
<valid_name>60E49542D19D16EDB633A40</valid_name>
....
I don't need for users to see in their element name !
, *
... I need to know what are characters that aren't allowed to be in element name, And I will replace them probably with an underscore, I am opened also if you have better proposition instead of replacing them with an underscore.
<customerDefinedField name="<?php echo htmlspecialchars($elementName); ?>">
might be sensible. – Quentin Jan 31 '20 at 9:24<row><item><name>the users name</name></item></row>
. Would save you a world of trouble with characters which are not allowed. – fredrik Jan 31 '20 at 9:24