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

Commit a0ed340

Browse files
committed
init project from inhere/librarys
0 parents  commit a0ed340

File tree

13 files changed

+1556
-0
lines changed

13 files changed

+1556
-0
lines changed

.editorconfig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
root = true
2+
3+
# 对所有文件生效
4+
[*]
5+
charset = utf-8
6+
indent_style = space
7+
indent_size = 2
8+
end_of_line = lf
9+
insert_final_newline = true
10+
trim_trailing_whitespace = true
11+
12+
# 对后缀名为 md 的文件生效
13+
[*.md]
14+
trim_trailing_whitespace = false
15+
16+
[*.php]
17+
indent_size = 4

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.idea/
2+
.phpintel/
3+
!README.md
4+
!.gitkeep
5+
composer.lock
6+
*.swp
7+
*.log
8+
*.pid
9+
*.patch
10+
.DS_Store

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 inhere
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# string utils for php
2+
3+
## license
4+
5+
MIT

composer.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "mylib/arr-utils",
3+
"type": "library",
4+
"description": "some array tool library of the php",
5+
"keywords": ["library","tool","php"],
6+
"homepage": "https://github.com/php-mylib/arr-utils",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "inhere",
11+
"email": "in.798@qq.com",
12+
"homepage": "http://www.yzone.net/"
13+
}
14+
],
15+
"require": {
16+
"php": ">=7.0.0"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"MyLib\\StrUtil\\" : "src/"
21+
}
22+
},
23+
"suggest": {
24+
"inhere/simple-print-tool": "Very lightweight data printing tools",
25+
"inhere/php-validate": "Very lightweight data validate tool",
26+
"inhere/console": "a lightweight php console application library."
27+
}
28+
}

phpunit.xml.dist

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
bootstrap="test/boot.php"
6+
colors="false"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
>
13+
<testsuites>
14+
<testsuite name="Php Library Test Suite">
15+
<directory>test/</directory>
16+
</testsuite>
17+
</testsuites>
18+
19+
<filter>
20+
<whitelist>
21+
<directory suffix=".php">src</directory>
22+
</whitelist>
23+
</filter>
24+
</phpunit>

src/HtmlHelper.php

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Inhere
5+
* Date: 2016/8/10 0010
6+
* Time: 00:44
7+
*/
8+
9+
namespace MyLib\StrUtil;
10+
11+
/**
12+
* Class HtmlHelper
13+
* @package MyLib\StrUtil
14+
*/
15+
class HtmlHelper
16+
{
17+
/**
18+
* Encodes special characters into HTML entities.
19+
* @param string $text data to be encoded
20+
* @param string $charset
21+
* @return string the encoded data
22+
* @see http://www.php.net/manual/en/function.htmlspecialchars.php
23+
*/
24+
public static function encode($text, $charset = 'utf-8')
25+
{
26+
return htmlspecialchars($text, ENT_QUOTES, $charset);
27+
}
28+
29+
/**
30+
* This is the opposite of {@link encode()}.
31+
* @param string $text data to be decoded
32+
* @return string the decoded data
33+
* @see http://www.php.net/manual/en/function.htmlspecialchars-decode.php
34+
*/
35+
public static function decode($text)
36+
{
37+
return htmlspecialchars_decode($text, ENT_QUOTES);
38+
}
39+
40+
/**
41+
* @form yii1
42+
* @param array $data data to be encoded
43+
* @param string $charset
44+
* @return array the encoded data
45+
* @see http://www.php.net/manual/en/function.htmlspecialchars.php
46+
*/
47+
public static function encodeArray($data, $charset = 'utf-8')
48+
{
49+
$d = [];
50+
51+
foreach ($data as $key => $value) {
52+
if (\is_string($key)) {
53+
$key = htmlspecialchars($key, ENT_QUOTES, $charset);
54+
}
55+
56+
if (\is_string($value)) {
57+
$value = htmlspecialchars($value, ENT_QUOTES, $charset);
58+
} elseif (\is_array($value)) {
59+
$value = static::encodeArray($value);
60+
}
61+
62+
$d[$key] = $value;
63+
}
64+
65+
return $d;
66+
}
67+
68+
69+
/**
70+
* html代码转义
71+
* htmlspecialchars 只转化这几个html [ & ' " < > ] 代码 --> [ &amp; &quot; ],
72+
* 而 htmlentities 却会转化所有的html代码,连同里面的它无法识别的中文字符也会转化。
73+
* 一般使用 htmlspecialchars 就足够了,要使用 htmlentities 时,要注意为第三个参数传递正确的编码。
74+
* htmlentities() <--> html_entity_decode() — 将特殊的 HTML 实体转换回普通字符
75+
* htmlspecialchars() <--> htmlspecialchars_decode() — 将特殊的 HTML 实体转换回普通字符
76+
* ENT_COMPAT ENT_QUOTES ENT_NOQUOTES ENT_HTML401 ENT_XML1 ENT_XHTML ENT_HTML5
77+
* @param $data
78+
* @param int $type
79+
* @param string $encoding
80+
* @return array|mixed|string
81+
*/
82+
public static function escape($data, int $type = 0, $encoding = 'UTF-8')
83+
{
84+
if (\is_array($data)) {
85+
foreach ($data as $k => $v) {
86+
$data[$k] = self::escape($data, $type, $encoding);
87+
}
88+
89+
} else {
90+
// 默认使用 htmlspecialchars()
91+
if (!$type) {
92+
$data = htmlspecialchars($data, ENT_QUOTES, $encoding);
93+
} else {
94+
$data = htmlentities($data, ENT_QUOTES, $encoding);
95+
}
96+
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+
}
102+
}
103+
104+
return $data;
105+
}
106+
107+
/**
108+
* 去掉html转义
109+
* @param $data
110+
* @param int $type
111+
* @param string $encoding
112+
* @return array|string
113+
*/
114+
public static function unescap($data, $type = 0, $encoding = 'UTF-8')
115+
{
116+
if (\is_array($data)) {
117+
foreach ($data as $k => $v) {
118+
$data[$k] = self::unescap($data, $type, $encoding);
119+
}
120+
121+
} else {
122+
if (!$type) {//默认使用 htmlspecialchars_decode()
123+
$data = htmlspecialchars_decode($data, ENT_QUOTES);
124+
} else {
125+
$data = html_entity_decode($data, ENT_QUOTES, $encoding);
126+
}
127+
}
128+
129+
return $data;
130+
}
131+
132+
/**
133+
* Strip img-tags from string
134+
* @param string $string Sting to be cleaned.
135+
* @return string Cleaned string
136+
*/
137+
public static function stripImages(string $string): string
138+
{
139+
return preg_replace('#(<[/]?img.*>)#U', '', $string);
140+
}
141+
142+
/**
143+
* Strip iframe-tags from string
144+
* @param string $string Sting to be cleaned.
145+
* @return string Cleaned string
146+
*/
147+
public static function stripIframes(string $string): string
148+
{
149+
return preg_replace('#(<[/]?iframe.*>)#U', '', $string);
150+
}
151+
152+
/**
153+
* stripScript
154+
* @param string $string
155+
* @return string
156+
*/
157+
public static function stripScript(string $string): string
158+
{
159+
return preg_replace('/<script[^>]*>.*?</script>/si', '', $string);
160+
}
161+
162+
/**
163+
* stripStyle
164+
* @param string $string
165+
* @return string
166+
*/
167+
public static function stripStyle(string $string): string
168+
{
169+
return preg_replace('/<style[^>]*>.*?</style>/si', '', $string);
170+
}
171+
172+
/**
173+
* @param string $html
174+
* @param bool|true $onlySrc
175+
* @return array
176+
*/
177+
public static function findImages(string $html, bool $onlySrc = true): array
178+
{
179+
// $preg = '/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*>/i';
180+
$preg = '/<img.+src=\"(:?.+.+\.(?:jpg|gif|bmp|bnp|png)\"?).+>/i';
181+
182+
if (!preg_match_all($preg, trim($html), $images)) {
183+
return [];
184+
}
185+
186+
if ($onlySrc) {
187+
return array_key_exists(1, $images) ? $images[1] : [];
188+
}
189+
190+
return $images;
191+
}
192+
193+
/**
194+
* @param string $html
195+
* @return string
196+
*/
197+
public static function minify(string $html): string
198+
{
199+
$search = [
200+
'/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\|\')\/\/.*))/',
201+
'/\n/',
202+
'/\>[^\S ]+/s',
203+
'/[^\S ]+\</s',
204+
'/(\s)+/s'
205+
];
206+
$replace = [' ', ' ', '>', '<', '\\1'];
207+
208+
return preg_replace($search, $replace, $html);
209+
}
210+
}

src/Json.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-10-25
6+
* Time: 9:41
7+
*/
8+
9+
namespace MyLib\StrUtil;
10+
11+
/**
12+
* Class Json
13+
* @package MyLib\StrUtil
14+
*/
15+
class Json extends JsonHelper
16+
{
17+
18+
}

0 commit comments

Comments
 (0)