[ Index ]

PHP Cross Reference of phpBB-3.3.7-deutsch

title

Body

[close]

/vendor/s9e/regexp-builder/src/ -> Escaper.php (source)

   1  <?php declare(strict_types=1);
   2  
   3  /**
   4  * @package   s9e\RegexpBuilder
   5  * @copyright Copyright (c) 2016-2021 The s9e authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\RegexpBuilder;
   9  
  10  class Escaper
  11  {
  12      /**
  13      * @var array Characters to escape in a character class
  14      */
  15      public $inCharacterClass = ['-' => '\\-', '\\' => '\\\\', ']' => '\\]', '^' => '\\^'];
  16  
  17      /**
  18      * @var array Characters to escape outside of a character class
  19      */
  20      public $inLiteral = [
  21          '$'  => '\\$',  '(' => '\\(', ')' => '\\)', '*' => '\\*',
  22          '+'  => '\\+',  '.' => '\\.', '?' => '\\?', '[' => '\\[',
  23          '\\' => '\\\\', '^' => '\\^', '{' => '\\{', '|' => '\\|'
  24      ];
  25  
  26      /**
  27      * @param string $delimiter Delimiter used in the final regexp
  28      */
  29  	public function __construct(string $delimiter = '/')
  30      {
  31          foreach (str_split($delimiter, 1) as $char)
  32          {
  33              $this->inCharacterClass[$char] = '\\' . $char;
  34              $this->inLiteral[$char]        = '\\' . $char;
  35          }
  36      }
  37  
  38      /**
  39      * Escape given character to be used in a character class
  40      *
  41      * @param  string $char Original character
  42      * @return string       Escaped character
  43      */
  44  	public function escapeCharacterClass(string $char): string
  45      {
  46          return $this->inCharacterClass[$char] ?? $char;
  47      }
  48  
  49      /**
  50      * Escape given character to be used outside of a character class
  51      *
  52      * @param  string $char Original character
  53      * @return string       Escaped character
  54      */
  55  	public function escapeLiteral(string $char): string
  56      {
  57          return $this->inLiteral[$char] ?? $char;
  58      }
  59  }


Generated: Thu Mar 24 21:31:15 2022 Cross-referenced by PHPXref 0.7.1