Elgg  Version 6.1
BalancedMatch.php
Go to the documentation of this file.
1 <?php
8 namespace CssCrush;
9 
10 #[\AllowDynamicProperties]
12 {
13  public function __construct(StringObject $string, $offset, $brackets = '{}')
14  {
15  $this->string = $string;
16  $this->offset = $offset;
17  $this->match = null;
18  $this->length = 0;
19 
20  list($opener, $closer) = str_split($brackets, 1);
21 
22  if (strpos($string->raw, $opener, $this->offset) === false) {
23 
24  return;
25  }
26 
27  if (substr_count($string->raw, $opener) !== substr_count($string->raw, $closer)) {
28  $sample = substr($string->raw, $this->offset, 25);
29  warning("Unmatched token near '$sample'.");
30 
31  return;
32  }
33 
34  $patt = ($opener === '{') ? Regex::$patt->block : Regex::$patt->parens;
35 
36  if (preg_match($patt, $string->raw, $m, PREG_OFFSET_CAPTURE, $this->offset)) {
37 
38  $this->match = $m;
39  $this->matchLength = strlen($m[0][0]);
40  $this->matchStart = $m[0][1];
41  $this->matchEnd = $this->matchStart + $this->matchLength;
42  $this->length = $this->matchEnd - $this->offset;
43  }
44  else {
45  warning("Could not match '$opener'. Exiting.");
46  }
47  }
48 
49  public function inside()
50  {
51  return $this->match[2][0];
52  }
53 
54  public function whole()
55  {
56  return substr($this->string->raw, $this->offset, $this->length);
57  }
58 
59  public function replace($replacement)
60  {
61  $this->string->splice($replacement, $this->offset, $this->length);
62  }
63 
64  public function unWrap()
65  {
66  $this->string->splice($this->inside(), $this->offset, $this->length);
67  }
68 }
if(empty($count)) $offset
Definition: pagination.php:26
Balanced bracket matching on string objects.
__construct(StringObject $string, $offset, $brackets= '{}')