ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
TransformUtil.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
27{
28 protected const PH_START = "{{{{{";
29 protected const PH_END = "}}}}}";
30
31 public function getPosOfPlaceholder(string $html, string $tag, int $offset = 0): ?int
32 {
33 $p = strpos($html, self::PH_START . $tag, $offset);
34 return is_int($p) ? $p : null;
35 }
36
37 public function getEndPosOfPlaceholder(string $html, int $offset = 0): ?int
38 {
39 $p = strpos($html, self::PH_END, $offset);
40 return is_int($p) ? ($p + strlen(self::PH_END)) : null;
41 }
42
43 public function getPlaceholderParamString(string $html, string $tag): ?string
44 {
45 $start = $this->getPosOfPlaceholder($html, $tag);
46 if (is_int($start)) {
47 $end = $this->getEndPosOfPlaceholder($html, $start);
48 $tag_string = substr($html, $start + strlen(self::PH_START), $end - $start - strlen(self::PH_START) - strlen(self::PH_END));
49 return $tag_string;
50 }
51 return null;
52 }
53
54 public function getPlaceholderParams(string $html, string $tag): ?array
55 {
56 $tag_string = $this->getPlaceholderParamString($html, $tag);
57 if (is_string($tag_string)) {
58 return explode(";", $tag_string);
59 }
60 return null;
61 }
62
67 public function getInnerContentOfPlaceholders(string $html, string $start_tag, string $end_tag): ?string
68 {
69 $start1 = $this->getPosOfPlaceholder($html, $start_tag);
70 if (is_int($start1)) {
71 $end1 = $this->getEndPosOfPlaceholder($html, $start1);
72 $param_str = $this->getPlaceholderParamString($html, $start_tag);
73 $end_tag_with_params = str_replace($start_tag . ";", $end_tag . ";", $param_str);
74 $start2 = $this->getPosOfPlaceholder($html, $end_tag_with_params);
75 if (is_int($end1) && is_int($start2)) {
76 $end2 = $this->getEndPosOfPlaceholder($html, $start2);
77 if (is_int($end2)) {
78 return substr($html, $end1, $start2 - $end1);
79 }
80 }
81 }
82 return null;
83 }
84
90 public function replaceInnerContentAndPlaceholders(string $html, string $start_tag, string $end_tag, string $replacement): ?string
91 {
92 $start1 = $this->getPosOfPlaceholder($html, $start_tag);
93 if (is_int($start1)) {
94 $end1 = $this->getEndPosOfPlaceholder($html, $start1);
95 $param_str = $this->getPlaceholderParamString($html, $start_tag);
96 $end_tag_with_params = str_replace($start_tag . ";", $end_tag . ";", $param_str);
97 $start2 = $this->getPosOfPlaceholder($html, $end_tag_with_params);
98 if (is_int($end1) && is_int($start2)) {
99 $end2 = $this->getEndPosOfPlaceholder($html, $start2);
100 if (is_int($end2)) {
101 return substr($html, 0, $start1) .
102 $replacement .
103 substr($html, $end2);
104 }
105 }
106 }
107 return null;
108 }
109
110
111}
getPosOfPlaceholder(string $html, string $tag, int $offset=0)
getPlaceholderParamString(string $html, string $tag)
getPlaceholderParams(string $html, string $tag)
getInnerContentOfPlaceholders(string $html, string $start_tag, string $end_tag)
parameters of start and end tag must match {{{{{StartTag;a;b;c}}}}}...inner content....
getEndPosOfPlaceholder(string $html, int $offset=0)
replaceInnerContentAndPlaceholders(string $html, string $start_tag, string $end_tag, string $replacement)
parameters of start and end tag must match {{{{{StartTag;a;b;c}}}}}...inner content....