[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/ -> Utils.php (source)

   1  <?php
   2  
   3  /*
   4  * @package   s9e\TextFormatter
   5  * @copyright Copyright (c) 2010-2019 The s9e Authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\TextFormatter;
   9  use DOMDocument;
  10  use DOMXPath;
  11  abstract class Utils
  12  {
  13  	public static function getAttributeValues($xml, $tagName, $attrName)
  14      {
  15          $values = [];
  16          if (\strpos($xml, '<' . $tagName) !== \false)
  17          {
  18              $regexp = '(<' . \preg_quote($tagName) . '(?= )[^>]*? ' . \preg_quote($attrName) . '="([^"]*+))';
  19              \preg_match_all($regexp, $xml, $matches);
  20              foreach ($matches[1] as $value)
  21                  $values[] = \html_entity_decode($value, \ENT_QUOTES, 'UTF-8');
  22          }
  23          return $values;
  24      }
  25  	public static function encodeUnicodeSupplementaryCharacters($str)
  26      {
  27          return \preg_replace_callback(
  28              '([\\xF0-\\xF4]...)S',
  29              __CLASS__ . '::encodeUnicodeSupplementaryCharactersCallback',
  30              $str
  31          );
  32      }
  33  	public static function removeFormatting($xml)
  34      {
  35          $dom   = self::loadXML($xml);
  36          $xpath = new DOMXPath($dom);
  37          foreach ($xpath->query('//e | //s') as $node)
  38              $node->parentNode->removeChild($node);
  39          return $dom->documentElement->textContent;
  40      }
  41  	public static function removeTag($xml, $tagName, $nestingLevel = 0)
  42      {
  43          if (\strpos($xml, '<' . $tagName) === \false)
  44              return $xml;
  45          $dom   = self::loadXML($xml);
  46          $xpath = new DOMXPath($dom);
  47          $query = '//' . $tagName . '[count(ancestor::' . $tagName . ') >= ' . $nestingLevel . ']';
  48          $nodes = $xpath->query($query);
  49          foreach ($nodes as $node)
  50              $node->parentNode->removeChild($node);
  51          return self::saveXML($dom);
  52      }
  53  	public static function replaceAttributes($xml, $tagName, callable $callback)
  54      {
  55          if (\strpos($xml, '<' . $tagName) === \false)
  56              return $xml;
  57          return \preg_replace_callback(
  58              '((<' . \preg_quote($tagName) . ')(?=[ />])[^>]*?(/?>))',
  59              function ($m) use ($callback)
  60              {
  61                  return $m[1] . self::serializeAttributes($callback(self::parseAttributes($m[0]))) . $m[2];
  62              },
  63              $xml
  64          );
  65      }
  66  	protected static function encodeUnicodeSupplementaryCharactersCallback(array $m)
  67      {
  68          $utf8 = $m[0];
  69          $cp   = (\ord($utf8[0]) << 18) + (\ord($utf8[1]) << 12) + (\ord($utf8[2]) << 6) + \ord($utf8[3]) - 0x3C82080;
  70          return '&#' . $cp . ';';
  71      }
  72  	protected static function loadXML($xml)
  73      {
  74          $flags = (\LIBXML_VERSION >= 20700) ? \LIBXML_COMPACT | \LIBXML_PARSEHUGE : 0;
  75          $dom = new DOMDocument;
  76          $dom->loadXML($xml, $flags);
  77          return $dom;
  78      }
  79  	protected static function parseAttributes($xml)
  80      {
  81          $attributes = [];
  82          if (\strpos($xml, '="') !== \false)
  83          {
  84              \preg_match_all('(([^ =]++)="([^"]*))S', $xml, $matches);
  85              foreach ($matches[1] as $i => $attrName)
  86                  $attributes[$attrName] = \html_entity_decode($matches[2][$i], \ENT_QUOTES, 'UTF-8');
  87          }
  88          return $attributes;
  89      }
  90  	protected static function saveXML(DOMDocument $dom)
  91      {
  92          return self::encodeUnicodeSupplementaryCharacters($dom->saveXML($dom->documentElement));
  93      }
  94  	protected static function serializeAttributes(array $attributes)
  95      {
  96          $xml = '';
  97          \ksort($attributes);
  98          foreach ($attributes as $attrName => $attrValue)
  99              $xml .= ' ' . \htmlspecialchars($attrName, \ENT_QUOTES) . '="' . \htmlspecialchars($attrValue, \ENT_COMPAT) . '"';
 100          $xml = \preg_replace('/\\r\\n?/', "\n", $xml);
 101          $xml = \preg_replace('/[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F]+/S', '', $xml);
 102          $xml = \str_replace("\n", '&#10;', $xml);
 103          return self::encodeUnicodeSupplementaryCharacters($xml);
 104      }
 105  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1