Skip to content
This repository was archived by the owner on Jun 11, 2020. It is now read-only.

Commit 0dc4660

Browse files
committed
format codes, update readme
1 parent fc33630 commit 0dc4660

File tree

6 files changed

+195
-179
lines changed

6 files changed

+195
-179
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
# string utils for php
1+
# str utils
2+
3+
string utils for php
4+
5+
contains:
6+
7+
- html string helper
8+
- json string helper
9+
- url string helper
10+
- common string helper
211

312
## install
413

composer.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
}
2828
},
2929
"suggest": {
30-
"inhere/simple-print-tool": "Very lightweight data printing tools",
3130
"inhere/php-validate": "Very lightweight data validate tool",
32-
"inhere/console": "a lightweight php console application library."
3331
}
3432
}

phpunit.xml.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
convertNoticesToExceptions="true"
99
convertWarningsToExceptions="true"
1010
stopOnFailure="false"
11-
1211
>
1312
<testsuites>
1413
<testsuite name="Php Library Test Suite">

src/HtmlHelper.php

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class HtmlHelper
2323
*/
2424
public static function encode($text, $charset = 'utf-8'): string
2525
{
26-
return htmlspecialchars($text, ENT_QUOTES, $charset);
26+
return \htmlspecialchars($text, ENT_QUOTES, $charset);
2727
}
2828

2929
/**
@@ -34,7 +34,7 @@ public static function encode($text, $charset = 'utf-8'): string
3434
*/
3535
public static function decode($text): string
3636
{
37-
return htmlspecialchars_decode($text, ENT_QUOTES);
37+
return \htmlspecialchars_decode($text, ENT_QUOTES);
3838
}
3939

4040
/**
@@ -50,11 +50,11 @@ public static function encodeArray($data, $charset = 'utf-8'): array
5050

5151
foreach ($data as $key => $value) {
5252
if (\is_string($key)) {
53-
$key = htmlspecialchars($key, ENT_QUOTES, $charset);
53+
$key = \htmlspecialchars($key, ENT_QUOTES, $charset);
5454
}
5555

5656
if (\is_string($value)) {
57-
$value = htmlspecialchars($value, ENT_QUOTES, $charset);
57+
$value = \htmlspecialchars($value, ENT_QUOTES, $charset);
5858
} elseif (\is_array($value)) {
5959
$value = static::encodeArray($value);
6060
}
@@ -86,19 +86,19 @@ public static function escape($data, int $type = 0, $encoding = 'UTF-8')
8686
$data[$k] = self::escape($data, $type, $encoding);
8787
}
8888

89+
return $data;
90+
}
91+
92+
// 默认使用 htmlspecialchars()
93+
if (!$type) {
94+
$data = \htmlspecialchars($data, \ENT_QUOTES, $encoding);
8995
} else {
90-
// 默认使用 htmlspecialchars()
91-
if (!$type) {
92-
$data = htmlspecialchars($data, ENT_QUOTES, $encoding);
93-
} else {
94-
$data = htmlentities($data, ENT_QUOTES, $encoding);
95-
}
96+
$data = \htmlentities($data, \ENT_QUOTES, $encoding);
97+
}
9698

97-
//如‘&#x5FD7;’这样的16进制的html字符,为了防止这样的字符被错误转译,使用正则进行匹配,把这样的字符又转换回来。
98-
if (strpos($data, '&#')) {
99-
$data = preg_replace('/&((#(\d{3,5}|x[a-fA-F0-9]{4}));)/',
100-
'&\\1', $data);
101-
}
99+
//如‘&#x5FD7;’这样的16进制的html字符,为了防止这样的字符被错误转译,使用正则进行匹配,把这样的字符又转换回来。
100+
if (\strpos($data, '&#')) {
101+
$data = \preg_replace('/&((#(\d{3,5}|x[a-fA-F0-9]{4}));)/', '&\\1', $data);
102102
}
103103

104104
return $data;
@@ -119,9 +119,9 @@ public static function unescap($data, $type = 0, $encoding = 'UTF-8')
119119
}
120120

121121
} elseif (!$type) {//默认使用 htmlspecialchars_decode()
122-
$data = htmlspecialchars_decode($data, ENT_QUOTES);
122+
$data = \htmlspecialchars_decode($data, \ENT_QUOTES);
123123
} else {
124-
$data = html_entity_decode($data, ENT_QUOTES, $encoding);
124+
$data = \html_entity_decode($data, \ENT_QUOTES, $encoding);
125125
}
126126

127127
return $data;
@@ -144,7 +144,7 @@ public static function stripImages(string $string): string
144144
*/
145145
public static function stripIframes(string $string): string
146146
{
147-
return preg_replace('#(<[/]?iframe.*>)#U', '', $string);
147+
return \preg_replace('#(<[/]?iframe.*>)#U', '', $string);
148148
}
149149

150150
/**
@@ -154,7 +154,7 @@ public static function stripIframes(string $string): string
154154
*/
155155
public static function stripScript(string $string): string
156156
{
157-
return preg_replace('/<script[^>]*>.*?</script>/si', '', $string);
157+
return \preg_replace('/<script[^>]*>.*?</script>/si', '', $string);
158158
}
159159

160160
/**
@@ -164,25 +164,25 @@ public static function stripScript(string $string): string
164164
*/
165165
public static function stripStyle(string $string): string
166166
{
167-
return preg_replace('/<style[^>]*>.*?</style>/si', '', $string);
167+
return \preg_replace('/<style[^>]*>.*?</style>/si', '', $string);
168168
}
169169

170170
/**
171171
* @param string $html
172172
* @param bool|true $onlySrc
173173
* @return array
174174
*/
175-
public static function findImages(string $html, bool $onlySrc = true): array
175+
public static function matchImages(string $html, bool $onlySrc = true): array
176176
{
177177
// $preg = '/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*>/i';
178178
$preg = '/<img.+src=\"(:?.+.+\.(?:jpg|gif|bmp|bnp|png)\"?).+>/i';
179179

180-
if (!preg_match_all($preg, trim($html), $images)) {
180+
if (!\preg_match_all($preg, trim($html), $images)) {
181181
return [];
182182
}
183183

184184
if ($onlySrc) {
185-
return array_key_exists(1, $images) ? $images[1] : [];
185+
return \array_key_exists(1, $images) ? $images[1] : [];
186186
}
187187

188188
return $images;
@@ -194,7 +194,7 @@ public static function findImages(string $html, bool $onlySrc = true): array
194194
*/
195195
public static function minify(string $html): string
196196
{
197-
$search = [
197+
$search = [
198198
'/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\|\')\/\/.*))/',
199199
'/\n/',
200200
'/\>[^\S ]+/s',
@@ -203,6 +203,6 @@ public static function minify(string $html): string
203203
];
204204
$replace = [' ', ' ', '>', '<', '\\1'];
205205

206-
return preg_replace($search, $replace, $html);
206+
return \preg_replace($search, $replace, $html);
207207
}
208208
}

src/JsonHelper.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,10 @@ public static function format($input, $output = false, array $options = [])
135135
$options = \array_merge($default, $options);
136136

137137
if (\file_exists($input) && (empty($options['file']) || !\is_file($options['file']))) {
138-
$dir = \dirname($input);
138+
$dir = \dirname($input);
139139
$name = \basename($input, '.json');
140140
$file = $dir . '/' . $name . '.' . $options['type'] . '.json';
141+
// save to options
141142
$options['file'] = $file;
142143
}
143144

@@ -155,18 +156,18 @@ public static function saveAs(string $data, string $output, array $options = [])
155156
{
156157
$default = ['type' => 'min', 'file' => ''];
157158
$options = array_merge($default, $options);
158-
$dir = \dirname($output);
159+
$saveDir = \dirname($output);
159160

160-
if (!\file_exists($dir)) {
161-
throw new \RuntimeException('设置的json文件输出' . $dir . '目录不存在!');
161+
if (!\file_exists($saveDir)) {
162+
throw new \RuntimeException('设置的json文件输出' . $saveDir . '目录不存在!');
162163
}
163164

164165
$name = \basename($output, '.json');
165-
$file = $dir . '/' . $name . '.' . $options['type'] . '.json';
166+
$file = $saveDir . '/' . $name . '.' . $options['type'] . '.json';
166167

168+
// 去掉空白
167169
if ($options['type '] === 'min') {
168-
// 去掉空白
169-
$data = (string)\preg_replace('/(?!\w)\s*?(?!\w)/i', '', $data);
170+
$data = \preg_replace('/(?!\w)\s*?(?!\w)/i', '', $data);
170171
}
171172

172173
return \file_put_contents($file, $data);

0 commit comments

Comments
 (0)