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

前端 未结 9 1553
失恋的感觉
失恋的感觉 2020-11-27 06:56

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

9条回答
  •  南笙
    南笙 (楼主)
    2020-11-27 07:02

    The expression below should match valid unicode element names excepting xml. Names that start or end with xml will still be allowed. This passes @toscho's äøñ test. The one thing I could not figure out a regex for was extenders. The xml element name spec says:

    [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar | Extender

    [5] Name ::= (Letter | '_' | ':') (NameChar)*

    But there's no clear definition for a unicode category or class containing extenders.

    ^[\p{L}_:][\p{N}\p{L}\p{Mc}.\-|:]*((?

提交回复
热议问题