PluginProbe
Tableberg – Simple Gutenberg Table Block / 0.5.3
Tableberg – Simple Gutenberg Table Block v0.5.3
1.1.5 1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.5 1.0.4 1.0.3 1.0.2 1.0.1 trunk 0.0.2 0.2.1 0.3.2 0.3.3 0.4.1 0.5.0 0.5.1 0.5.2 0.5.3 0.5.4 0.5.5 0.5.6 0.5.7 All 42 releases
tableberg / includes / Utils / HtmlUtils.php

HtmlUtils.php in Tableberg – Simple Gutenberg Table Block 0.5.3, at includes/Utils/HtmlUtils.php

143 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Tableberg\Utils;
4
5 class HtmlUtils
6 {
7
8 /**
9 * Insert code in the beginning of the offset'th specified tag
10 *
11 * @param string $content
12 * @param string $tag
13 * @param string $code
14 * @param int $offset
15 * @return string new content with added attributes
16 */
17 public static function insert_inside_tag($content, $tag, $code, $offset = 0)
18 {
19 $tag = '<' . $tag;
20 $idx = strpos($content, $tag, $offset);
21 if ($idx === false) {
22 return $content;
23 }
24 $lidx = strpos($content, '>', $idx + 1);
25 if ($lidx === false) {
26 return $content;
27 }
28 return substr_replace($content, $code, $lidx + 1, 0);
29 }
30 /**
31 * Add attributes to the offset'th specified tag
32 *
33 * @param string $content
34 * @param string $tag
35 * @param string $attr_str
36 * @param int $offset
37 * @return string new content with added attributes
38 */
39 public static function add_attrs_to_tag($content, $tag, $attr_str, $offset = 0)
40 {
41 $tag = '<' . $tag;
42 $idx = strpos($content, $tag, $offset);
43 if ($idx === false) {
44 return $content;
45 }
46 return substr_replace($content, $tag . " " . $attr_str." ", $idx, strlen($tag) + 1);
47 }
48
49 /**
50 * Replace attributes of the offset'th specified tag
51 *
52 * @param string $content
53 * @param string $tag
54 * @param string $attr_str
55 * @param int $offset
56 * @return string new content with added attributes
57 */
58 public static function replace_attrs_of_tag($content, $tag, $attr_str, $offset = 0)
59 {
60 $tag = '<' . $tag;
61 $fidx = strpos($content, $tag, $offset);
62 if ($fidx === false) {
63 return $content;
64 }
65 $lidx = strpos($content, '>', $fidx);
66 if ($lidx === false) {
67 return $content;
68 }
69 return substr_replace($content, $tag . " " . $attr_str . ">", $fidx, $lidx - $fidx + 1);
70 }
71
72 /**
73 * Replace closing of the offset'th specified tag
74 *
75 * @param string $content
76 * @param string $tag
77 * @param string $replacement
78 * @param int $offset
79 * @return string new content with added attributes
80 */
81 public static function replace_closing_tag($content, $tag, $replacement, $offset = -1)
82 {
83 $tag = '</' . $tag . '>';
84 $idx = strrpos($content, $tag, $offset);
85 if ($idx === false) {
86 return $content;
87 }
88
89 return substr_replace($content, $replacement, $idx, strlen($tag));
90 }
91
92 /**
93 * Replace closing of the offset'th specified tag
94 *
95 * @param string $content
96 * @param string $tag
97 * @param string $replacement
98 * @param int $offset
99 * @return string new content with added attributes
100 */
101 public static function replace_starting_tag($content, $tag, $replacement, $offset = 0)
102 {
103 $tag = '<' . $tag;
104 $idx = strpos($content, $tag, $offset);
105 if ($idx === false) {
106 return $content;
107 }
108
109 return substr_replace($content, $replacement, $idx, strlen($tag));
110 }
111
112 /**
113 * Append attribute value to the offset'th specified tag
114 * <div class"abc"></div> -> <div class"abc newclass"></div>
115 *
116 * @param string $content
117 * @param string $tag
118 * @param string $value
119 * @param string $attr
120 * @param int $offset
121 * @return string new content with added attribute
122 */
123 public static function append_attr_value($content, $tag, $value, $attr, $offset = 0, &$cursor = false)
124 {
125 $idx = strpos($content, '<' . $tag, $offset);
126 if ($idx === false) {
127 return $content;
128 }
129 $lidx = strpos($content, '>', $idx + 1);
130 if ($lidx === false) {
131 return $content;
132 }
133 $cursor = $lidx;
134 $tagDes = substr($content, $idx, $lidx - $idx);
135 $count = 0;
136 $tagDes = preg_replace("/$attr=\"(.*?)\"/", "$attr=\"$1" . $value . "\"", $tagDes, -1, $count);
137 if ($count == 0) {
138 $tagDes = substr_replace($tagDes, " $attr=\"$value\"", strlen($tag) + 1, 0);
139 }
140 return substr_replace($content, $tagDes, $idx, $lidx - $idx);
141 }
142
143 }