How to check if string is a valid XML element name?

Posted by xsaero00 on Stack Overflow See other posts from Stack Overflow or by xsaero00
Published on 2010-03-25T22:20:57Z Indexed on 2010/03/25 22:23 UTC
Read the original article Hit count: 375

Filed under:
|
|

I need a regex or a function in PHP that will validate a string to be a good XML element name.

Form w3schools:

XML elements must follow these naming rules:

  1. Names can contain letters, numbers, and other characters
  2. Names cannot start with a number or punctuation character
  3. Names cannot start with the letters xml (or XML, or Xml, etc)
  4. Names cannot contain spaces

I can write a basic regex that will check for rules 1,2 and 4, but it won't account for all punctuation allowed and won't account for 3rd rule

\w[\w0-9-]

© Stack Overflow or respective owner

Related posts about php

Related posts about regex