FluentDOM\QualifiedName::normalizeString PHP Method

normalizeString() public static method

If the result of that removal is an empty string, the default value is returned.
public static normalizeString ( string $string, string $default = '_' ) : string
$string string
$default string
return string
    public static function normalizeString($string, $default = '_')
    {
        $nameStartChar = 'A-Z_a-z' . '\\x{C0}-\\x{D6}\\x{D8}-\\x{F6}\\x{F8}-\\x{2FF}\\x{370}-\\x{37D}' . '\\x{37F}-\\x{1FFF}\\x{200C}-\\x{200D}\\x{2070}-\\x{218F}' . '\\x{2C00}-\\x{2FEF}\\x{3001}-\\x{D7FF}\\x{F900}-\\x{FDCF}' . '\\x{FDF0}-\\x{FFFD}\\x{10000}-\\x{EFFFF}';
        $nameAdditionalChar = $nameStartChar . '\\.\\d\\x{B7}\\x{300}-\\x{36F}\\x{203F}-\\x{2040}';
        $result = preg_replace(array('([^' . $nameAdditionalChar . '-]+)u', '(^[^' . $nameStartChar . ']+)u'), '', $string);
        return empty($result) ? $default : $result;
    }

Usage Example

Beispiel #1
0
 /**
  * @param \DOMNode|Element $node
  * @param \DOMNode|Document|Element $target
  */
 private function transferNode(\DOMNode $node, \DOMNode $target)
 {
     if ($node->namespaceURI === self::XMLNS_JSONX) {
         if ($target instanceof Document) {
             $normalizedName = $name = 'json:json';
         } else {
             $name = $node->getAttribute('name');
             $normalizedName = QualifiedName::normalizeString($name, self::DEFAULT_QNAME);
         }
         $type = $node->localName;
         $newNode = $target->appendElement($normalizedName);
         if ($name !== $normalizedName && $name !== '') {
             $newNode->setAttribute('json:name', $name);
         }
         switch ($type) {
             case 'object':
                 if ($node('count(*) > 0')) {
                     foreach ($node('*') as $childNode) {
                         $this->transferNode($childNode, $newNode);
                     }
                     return;
                 }
                 break;
             case 'array':
                 foreach ($node('*') as $childNode) {
                     $this->transferNode($childNode, $newNode);
                 }
                 break;
             case 'string':
                 $newNode->append((string) $node);
                 return;
             default:
                 $newNode->append((string) $node);
                 break;
         }
         $newNode->setAttribute('json:type', $type);
     }
 }
All Usage Examples Of FluentDOM\QualifiedName::normalizeString