PluginProbe
Tableberg – Simple Gutenberg Table Block / 0.4.1
Tableberg – Simple Gutenberg Table Block v0.4.1
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 0.5.8 All 41 releases
tableberg / includes / Utils / HtmlUtils.php

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

123 lines 3.6 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 * Append attribute value to the offset'th specified tag
94 * <div class"abc"></div> -> <div class"abc newclass"></div>
95 *
96 * @param string $content
97 * @param string $tag
98 * @param string $add
99 * @param string $attr
100 * @param int $offset
101 * @return string new content with added classes
102 */
103 public static function append_attr_value($content, $tag, $add, $attr, $offset = 0, &$cursor = false)
104 {
105 $idx = strpos($content, '<' . $tag, $offset);
106 if ($idx === false) {
107 return $content;
108 }
109 $lidx = strpos($content, '>', $idx + 1);
110 if ($lidx === false) {
111 return $content;
112 }
113 $cursor = $lidx;
114 $tagDes = substr($content, $idx, $lidx - $idx);
115 $count = 0;
116 $tagDes = preg_replace("/$attr=\"(.*)\"/", "$attr=\"$1" . $add . "\"", $tagDes, -1, $count);
117 if ($count == 0) {
118 $tagDes = substr_replace($tagDes, " $attr=\"$add\"", strlen($tag) + 1, 0);
119 }
120 return substr_replace($content, $tagDes, $idx, $lidx - $idx);
121 }
122
123 }