1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
<?php
class A {
/**
* 解析html字符串(断句、不翻译纯数字)
* @param string $str 待解析文本
* @return array 解析出的字符串(排除html标签,断句等)
*/
public static function resolveHtml($str = '')
{
$return = [];
//第一个位置的标记
$pos = strpos($str, '<');
if ($pos) {
$posText = substr($str, 0, $pos);
if (!empty($posText) && !ctype_space($posText) && preg_match('/[\x7f-\xff_A-Za-z]/', $posText)) {
$posText = explode("\n", str_replace("\r", "\n", $posText));
$posText = array_filter($posText);
$return = array_merge($return, $posText);
}
}
//检测结束标记
$str = substr($str, $pos);
$startPos = strpos($str, '>');
if (!$startPos) {
if (!empty($str) && !ctype_space($str) && preg_match('/[\x7f-\xff_A-Za-z]/', $str)) {
$text = explode("\n", str_replace("\r", "\n", $str));
$text = array_filter($text);
$return = array_merge($return, $text);
}
return $return;
}
//递归处理文本字符
$str = substr($str, $startPos + 1);
$endPos = strpos($str, '<');
$text = substr($str, 0, $endPos);
if (!empty($text) && !ctype_space($text) && preg_match('/[\x7f-\xff_A-Za-z]/', $text)) {
$text = explode("\n", str_replace("\r", "\n", $text));
$text = array_filter($text);
$return = array_merge($return, $text);
}
$resolve = self::resolveHtml(substr($str, $endPos));
return $resolve ? array_merge($return, $resolve) : $return;
}
/**
* 还原回标签
* @param string $str 文本
* @param array $resolve 解析出的纯字符数组
* @param array $translate 纯字符翻译后的数组
* @return string 拼装翻译后的文本
*/
public static function resolveBack($str, $resolve, $translate)
{
if (!$resolve) {
return $str;
}
$result = '';
foreach ($resolve as $key=>$item)
{
//$str = str_replace($item, $translate[$key], $str);
//非贪婪匹配替换
$pos = strpos($str, $item) + mb_strlen($item);
$prefixStr = mb_substr($str, 0, $pos); //记录需要替换的字符串
$str = mb_substr($str, $pos); //记录剩余字符串
$prefixStr = str_replace($item, $translate[$key], $prefixStr);
$result .= $prefixStr;
}
return $result;
}
}
|