i7h   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 44
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A i18n() 0 22 2
A __construct() 0 1 1
1
<?php
2
3
/**
4
*  ___       _____      ________      ________       ________      ___           ___      ________       ___  ___          ___       __       ___      _________    ___  ___          ________    ___  ___      ________   
5
* |\  \     / __  \    |\   __  \    |\   ___  \    |\   ____\    |\  \         |\  \    |\   ____\     |\  \|\  \        |\  \     |\  \    |\  \    |\___   ___\ |\  \|\  \        |\   __  \  |\  \|\  \    |\   __  \  
6
* \ \  \   |\/_|\  \   \ \  \|\  \   \ \  \\ \  \   \ \  \___|    \ \  \        \ \  \   \ \  \___|_    \ \  \\\  \       \ \  \    \ \  \   \ \  \   \|___ \  \_| \ \  \\\  \       \ \  \|\  \ \ \  \\\  \   \ \  \|\  \ 
7
*  \ \  \  \|/ \ \  \   \ \   __  \   \ \  \\ \  \   \ \  \  ___   \ \  \        \ \  \   \ \_____  \    \ \   __  \       \ \  \  __\ \  \   \ \  \       \ \  \   \ \   __  \       \ \   ____\ \ \   __  \   \ \   ____\
8
*   \ \  \      \ \  \   \ \  \|\  \   \ \  \\ \  \   \ \  \|\  \   \ \  \____    \ \  \   \|____|\  \    \ \  \ \  \       \ \  \|\__\_\  \   \ \  \       \ \  \   \ \  \ \  \       \ \  \___|  \ \  \ \  \   \ \  \___|
9
*    \ \__\      \ \__\   \ \_______\   \ \__\\ \__\   \ \_______\   \ \_______\   \ \__\    ____\_\  \    \ \__\ \__\       \ \____________\   \ \__\       \ \__\   \ \__\ \__\       \ \__\      \ \__\ \__\   \ \__\   
10
*     \|__|       \|__|    \|_______|    \|__| \|__|    \|_______|    \|_______|    \|__|   |\_________\    \|__|\|__|        \|____________|    \|__|        \|__|    \|__|\|__|        \|__|       \|__|\|__|    \|__|   
11
*                                                                                           \|_________|                                                                                                                   
12
*                                                                                                                                                                                                                          
13
* 在 PHP 中实现莉沫酱自创的 i18nglish,且不用担心 XSS 攻击。
14
* Implement RimoChan's own i18nglish in PHP without worrying about XSS attacks.
15
* 
16
* @package i7h-PHP
17
* @version 0.5.0
18
* @author Mike Wang (Diamochang)
19
* @license MIT
20
*/
21
class i7h {
22
23
    /**
24
     * 这是一个构造函数。
25
     * 目前未包含特定初始化逻辑,可扩展以适应未来需求。
26
     * 
27
     * This is just a constructor.
28
     * It does not currently contain specific initialization logic and can be extended to accommodate future needs.
29
     */
30
    public function __construct() {
31
    }
32
33
    /**
34
     * 处理并返回安全的 i18nglish 字符串。
35
     * Processes and returns a safe i18nglish string.
36
     *
37
     * 此方法首先对输入字符串进行 XSS 防护处理,然后转换成 i18nglish 并输出。
38
     * This method first XSS-protects the input string, then converts it to i18nglish and outputs it.
39
     * 
40
     * @param string $str 待处理的原始字符串。The original string to be processed.
41
     * @return string 经过 XSS 过滤及 i18nglish 转换后的安全字符串。Safe strings after XSS filtering and i18nglish conversion.
42
     */
43
    public function i18n($str) {
44
        // 使用 htmlspecialchars 函数转义 HTML 实体,防止 XSS 攻击
45
        // Prevent XSS attacks by escaping HTML entities with the htmlspecialchars function
46
        $safeStr = htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
47
48
        // 正则表达式匹配单词,对匹配到的每个单词应用转换逻辑
49
        // The regular expression matches words and applies conversion logic to each word it matches
50
        $pattern = '/\b(?![\'-])\w+(?<![\'-])\b/u'; // 'u'修饰符用于支持 Unicode 字符 The 'u' modifier is used to support Unicode characters
51
        $callback = function ($matches) {
52
            $match = $matches[0];
53
            // 若单词长度超过2,则进行转换
54
            // If the word length is more than 2, the conversion is performed
55
            if (strlen($match) > 2) {
56
                return $match[0] . (strlen($match) - 2) . $match[-1];
57
            }
58
            return $match; // 单词长度小于等于2,直接返回原单词 If the length of the word is less than or equal to 2, return the original word directly
59
        };
60
        
61
        // 应用转换逻辑 Apply conversion logic
62
        $i7hResult = preg_replace_callback($pattern, $callback, $safeStr);
63
64
        return $i7hResult;
65
    }
66
}
67
68
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...