|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpBoot\DB; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* 剪出字符串中的嵌套字符串 |
|
7
|
|
|
* 既从aaa"bb\"b"ccc中, 取出"bb\"b" |
|
8
|
|
|
* @author caoym |
|
9
|
|
|
*/ |
|
10
|
|
|
class NestedStringCut{ |
|
11
|
|
|
|
|
12
|
2 |
|
public function __construct($str){ |
|
13
|
|
|
|
|
14
|
2 |
|
$pos = 0; |
|
15
|
2 |
|
$state = 'stateNormal'; |
|
16
|
2 |
|
while (true){ |
|
17
|
2 |
|
$pos = $this->$state($str, $pos, $state); |
|
18
|
2 |
|
if($pos === false){ |
|
19
|
2 |
|
break; |
|
20
|
|
|
} |
|
21
|
|
|
}; |
|
22
|
2 |
|
return false; |
|
|
|
|
|
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function getSnippets(){ |
|
26
|
|
|
return $this->snippets; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
2 |
|
public function getText(){ |
|
30
|
2 |
|
return implode('', $this->snippets); |
|
31
|
|
|
} |
|
32
|
|
|
/** |
|
33
|
|
|
* 将剪切后的字符串位置转换成原始字符串位置 |
|
34
|
|
|
* @param int $pos |
|
35
|
|
|
* @param int |
|
36
|
|
|
*/ |
|
37
|
2 |
|
public function mapPos($pos){ |
|
38
|
|
|
|
|
39
|
2 |
|
foreach ($this->snippets as $k => $v){ |
|
40
|
2 |
|
$pos += $k; |
|
41
|
2 |
|
if($pos < $k + strlen($v)){ |
|
42
|
2 |
|
break; |
|
43
|
|
|
} |
|
44
|
|
|
$pos -= ($k + strlen($v)); |
|
45
|
|
|
|
|
46
|
2 |
|
} |
|
47
|
2 |
|
return $pos; |
|
48
|
|
|
} |
|
49
|
|
|
/** |
|
50
|
|
|
* 普通状态 |
|
51
|
|
|
*/ |
|
52
|
2 |
|
private function stateNormal($str, $pos, &$next){ |
|
53
|
2 |
|
$ori = $pos; |
|
54
|
2 |
|
$posSQ = strpos($str, '\'', $pos); |
|
55
|
2 |
|
$posDQ = strpos($str, '"', $pos); |
|
56
|
2 |
|
$pos = $posSQ; |
|
57
|
2 |
|
$this->subStateQ = '\''; |
|
58
|
2 |
|
$next = 'stateQ'; |
|
59
|
2 |
|
if($posDQ !== false && (($posDQ < $pos) || ($pos === false)) ){ |
|
60
|
|
|
$pos = $posDQ; |
|
61
|
|
|
$this->subStateQ = '"'; |
|
62
|
|
|
} |
|
63
|
2 |
|
if($pos !== false){ |
|
64
|
|
|
$this->snippets[$ori] = substr($str, $ori, $pos-$ori); |
|
65
|
|
|
$pos ++; |
|
66
|
|
|
}else{ |
|
67
|
2 |
|
$this->snippets[$ori] = substr($str, $ori); |
|
68
|
|
|
} |
|
69
|
2 |
|
return $pos; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* 进入引号状态 |
|
74
|
|
|
*/ |
|
75
|
|
|
private function stateQ($str, $pos, &$next){ |
|
76
|
|
|
$posESC = strpos($str, '\\', $pos); |
|
77
|
|
|
$posQ = strpos($str, $this->subStateQ, $pos); |
|
78
|
|
|
$pos = $posESC; |
|
79
|
|
|
$next = 'stateESC'; |
|
80
|
|
|
|
|
81
|
|
|
if($posQ !== false && (($posQ<$posESC) || ($posESC === false))){ |
|
82
|
|
|
$pos = $posQ; |
|
83
|
|
|
$next = 'stateNormal'; |
|
84
|
|
|
} |
|
85
|
|
|
if($pos !== false){ |
|
86
|
|
|
$pos ++; |
|
87
|
|
|
} |
|
88
|
|
|
return $pos; |
|
89
|
|
|
} |
|
90
|
|
|
/** |
|
91
|
|
|
* 进入转义状态 |
|
92
|
|
|
*/ |
|
93
|
|
|
private function stateESC($str, $pos, &$next){ |
|
94
|
|
|
$pos++; |
|
95
|
|
|
if($pos >= strlen($str)){ |
|
96
|
|
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
$next = 'stateQ'; |
|
99
|
|
|
return $pos; |
|
100
|
|
|
} |
|
101
|
|
|
/** |
|
102
|
|
|
* 去掉嵌套字符串后的内容 |
|
103
|
|
|
* @var array |
|
104
|
|
|
*/ |
|
105
|
|
|
private $snippets=array(); |
|
106
|
|
|
|
|
107
|
|
|
private $subStateQ; |
|
108
|
|
|
} |
|
109
|
|
|
|