Skip to content

Commit

Permalink
format codes, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 9, 2019
1 parent 5746e19 commit d7bd02a
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 179 deletions.
11 changes: 10 additions & 1 deletion libs/str-utils/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
# string utils for php
# str utils

string utils for php

contains:

- html string helper
- json string helper
- url string helper
- common string helper

## install

Expand Down
2 changes: 0 additions & 2 deletions libs/str-utils/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
}
},
"suggest": {
"inhere/simple-print-tool": "Very lightweight data printing tools",
"inhere/php-validate": "Very lightweight data validate tool",
"inhere/console": "a lightweight php console application library."
}
}
1 change: 0 additions & 1 deletion libs/str-utils/phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false"

>
<testsuites>
<testsuite name="Php Library Test Suite">
Expand Down
50 changes: 25 additions & 25 deletions libs/str-utils/src/HtmlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class HtmlHelper
*/
public static function encode($text, $charset = 'utf-8'): string
{
return htmlspecialchars($text, ENT_QUOTES, $charset);
return \htmlspecialchars($text, ENT_QUOTES, $charset);
}

/**
Expand All @@ -34,7 +34,7 @@ public static function encode($text, $charset = 'utf-8'): string
*/
public static function decode($text): string
{
return htmlspecialchars_decode($text, ENT_QUOTES);
return \htmlspecialchars_decode($text, ENT_QUOTES);
}

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

foreach ($data as $key => $value) {
if (\is_string($key)) {
$key = htmlspecialchars($key, ENT_QUOTES, $charset);
$key = \htmlspecialchars($key, ENT_QUOTES, $charset);
}

if (\is_string($value)) {
$value = htmlspecialchars($value, ENT_QUOTES, $charset);
$value = \htmlspecialchars($value, ENT_QUOTES, $charset);
} elseif (\is_array($value)) {
$value = static::encodeArray($value);
}
Expand Down Expand Up @@ -86,19 +86,19 @@ public static function escape($data, int $type = 0, $encoding = 'UTF-8')
$data[$k] = self::escape($data, $type, $encoding);
}

return $data;
}

// 默认使用 htmlspecialchars()
if (!$type) {
$data = \htmlspecialchars($data, \ENT_QUOTES, $encoding);
} else {
// 默认使用 htmlspecialchars()
if (!$type) {
$data = htmlspecialchars($data, ENT_QUOTES, $encoding);
} else {
$data = htmlentities($data, ENT_QUOTES, $encoding);
}
$data = \htmlentities($data, \ENT_QUOTES, $encoding);
}

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

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

} elseif (!$type) {//默认使用 htmlspecialchars_decode()
$data = htmlspecialchars_decode($data, ENT_QUOTES);
$data = \htmlspecialchars_decode($data, \ENT_QUOTES);
} else {
$data = html_entity_decode($data, ENT_QUOTES, $encoding);
$data = \html_entity_decode($data, \ENT_QUOTES, $encoding);
}

return $data;
Expand All @@ -144,7 +144,7 @@ public static function stripImages(string $string): string
*/
public static function stripIframes(string $string): string
{
return preg_replace('#(<[/]?iframe.*>)#U', '', $string);
return \preg_replace('#(<[/]?iframe.*>)#U', '', $string);
}

/**
Expand All @@ -154,7 +154,7 @@ public static function stripIframes(string $string): string
*/
public static function stripScript(string $string): string
{
return preg_replace('/<script[^>]*>.*?</script>/si', '', $string);
return \preg_replace('/<script[^>]*>.*?</script>/si', '', $string);
}

/**
Expand All @@ -164,25 +164,25 @@ public static function stripScript(string $string): string
*/
public static function stripStyle(string $string): string
{
return preg_replace('/<style[^>]*>.*?</style>/si', '', $string);
return \preg_replace('/<style[^>]*>.*?</style>/si', '', $string);
}

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

if (!preg_match_all($preg, trim($html), $images)) {
if (!\preg_match_all($preg, trim($html), $images)) {
return [];
}

if ($onlySrc) {
return array_key_exists(1, $images) ? $images[1] : [];
return \array_key_exists(1, $images) ? $images[1] : [];
}

return $images;
Expand All @@ -194,7 +194,7 @@ public static function findImages(string $html, bool $onlySrc = true): array
*/
public static function minify(string $html): string
{
$search = [
$search = [
'/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\|\')\/\/.*))/',
'/\n/',
'/\>[^\S ]+/s',
Expand All @@ -203,6 +203,6 @@ public static function minify(string $html): string
];
$replace = [' ', ' ', '>', '<', '\\1'];

return preg_replace($search, $replace, $html);
return \preg_replace($search, $replace, $html);
}
}
15 changes: 8 additions & 7 deletions libs/str-utils/src/JsonHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,10 @@ public static function format($input, $output = false, array $options = [])
$options = \array_merge($default, $options);

if (\file_exists($input) && (empty($options['file']) || !\is_file($options['file']))) {
$dir = \dirname($input);
$dir = \dirname($input);
$name = \basename($input, '.json');
$file = $dir . '/' . $name . '.' . $options['type'] . '.json';
// save to options
$options['file'] = $file;
}

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

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

$name = \basename($output, '.json');
$file = $dir . '/' . $name . '.' . $options['type'] . '.json';
$file = $saveDir . '/' . $name . '.' . $options['type'] . '.json';

// 去掉空白
if ($options['type '] === 'min') {
// 去掉空白
$data = (string)\preg_replace('/(?!\w)\s*?(?!\w)/i', '', $data);
$data = \preg_replace('/(?!\w)\s*?(?!\w)/i', '', $data);
}

return \file_put_contents($file, $data);
Expand Down
Loading

0 comments on commit d7bd02a

Please sign in to comment.