PHP截取字符串乱码问题彻底解决

妈的,点真背,老子做了一个新站点,第一天上线就让百度收录了,并且排名很靠前,结果发现,第三天就给我K 掉了,我又没作弊,也没有做违法内容,MB滴百度,操他大爷,气S!!!就如同我的WEB开发笔记www.chhua.com,天天更新也是排名不好,怀疑百度有没有天理了!

好了,不发牢骚了,分享一个PHP截取字符串无乱码的函数,其实,这是UCHOME里的函数,只是稍微做了一下修改而已,有人说mb_substr()也可以,不是我信不过,当然可以,只是在mb_substr()是按字符来截的,不是按字节,OK 下面上代码:

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
<span style="color: #000000;font-weight: bold">function</span> cutstr<span style="color: #009900">(</span><span style="color: #000088">$string</span><span style="color: #339933">,</span> <span style="color: #000088">$length</span><span style="color: #339933">,</span><span style="color: #000088">$charset</span><span style="color: #339933">,</span><span style="color: #000088">$dot</span><span style="color: #009900">)</span> <span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//字符,截取长度,字符集,结尾符</span>
	<span style="color: #b1b100">if</span><span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/strlen" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">strlen</span></a><span style="color: #009900">(</span><span style="color: #000088">$string</span><span style="color: #009900">)</span> <span style="color: #339933">&</span>lt<span style="color: #339933">;=</span> <span style="color: #000088">$length</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
		<span style="color: #b1b100">return</span> <span style="color: #000088">$string</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000088">$pre</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/chr" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">chr</span></a><span style="color: #009900">(</span><span style="color: #cc66cc">1</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
	<span style="color: #000088">$end</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/chr" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">chr</span></a><span style="color: #009900">(</span><span style="color: #cc66cc">1</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
	<span style="color: #666666;font-style: italic">//保护特殊字符串</span>
	<span style="color: #000088">$string</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/str_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">str_replace</span></a><span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array</span></a><span style="color: #009900">(</span><span style="color: #0000ff">'&amp;'</span><span style="color: #339933">,</span> <span style="color: #0000ff">'"'</span><span style="color: #339933">,</span> <span style="color: #0000ff">'&lt;'</span><span style="color: #339933">,</span> <span style="color: #0000ff">'&gt;'</span><span style="color: #009900">)</span><span style="color: #339933">,</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array</span></a><span style="color: #009900">(</span><span style="color: #000088">$pre</span><span style="color: #339933">.</span><span style="color: #0000ff">'&amp;'</span><span style="color: #339933">.</span><span style="color: #000088">$end</span><span style="color: #339933">,</span> <span style="color: #000088">$pre</span><span style="color: #339933">.</span><span style="color: #0000ff">'"'</span><span style="color: #339933">.</span><span style="color: #000088">$end</span><span style="color: #339933">,</span> <span style="color: #000088">$pre</span><span style="color: #339933">.</span><span style="color: #0000ff">'&lt;'</span><span style="color: #339933">.</span><span style="color: #000088">$end</span><span style="color: #339933">,</span> <span style="color: #000088">$pre</span><span style="color: #339933">.</span><span style="color: #0000ff">'&gt;'</span><span style="color: #339933">.</span><span style="color: #000088">$end</span><span style="color: #009900">)</span><span style="color: #339933">,</span> <span style="color: #000088">$string</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
 
	<span style="color: #000088">$strcut</span> <span style="color: #339933">=</span> <span style="color: #0000ff">''</span><span style="color: #339933">;</span>
	<span style="color: #b1b100">if</span><span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/strtolower" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">strtolower</span></a><span style="color: #009900">(</span><span style="color: #000088">$charset</span><span style="color: #009900">)</span> <span style="color: #339933">==</span> <span style="color: #0000ff">'utf-8'</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
		<span style="color: #000088">$n</span> <span style="color: #339933">=</span> <span style="color: #000088">$tn</span> <span style="color: #339933">=</span> <span style="color: #000088">$noc</span> <span style="color: #339933">=</span> <span style="color: #cc66cc">0</span><span style="color: #339933">;</span>
		<span style="color: #b1b100">while</span><span style="color: #009900">(</span><span style="color: #000088">$n</span> <span style="color: #339933">&</span>lt<span style="color: #339933">;</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/strlen" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">strlen</span></a><span style="color: #009900">(</span><span style="color: #000088">$string</span><span style="color: #009900">)</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
 
			<span style="color: #000088">$t</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/ord" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">ord</span></a><span style="color: #009900">(</span><span style="color: #000088">$string</span><span style="color: #009900">[</span><span style="color: #000088">$n</span><span style="color: #009900">]</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
			<span style="color: #b1b100">if</span><span style="color: #009900">(</span><span style="color: #000088">$t</span> <span style="color: #339933">==</span> <span style="color: #cc66cc">9</span> <span style="color: #339933">||</span> <span style="color: #000088">$t</span> <span style="color: #339933">==</span> <span style="color: #cc66cc">10</span> <span style="color: #339933">||</span> <span style="color: #009900">(</span><span style="color: #cc66cc">32</span> <span style="color: #339933">&</span>lt<span style="color: #339933">;=</span> <span style="color: #000088">$t</span> <span style="color: #339933">&</span>amp<span style="color: #339933">;&</span>amp<span style="color: #339933">;</span> <span style="color: #000088">$t</span> <span style="color: #339933">&</span>lt<span style="color: #339933">;=</span> <span style="color: #cc66cc">126</span><span style="color: #009900">)</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
				<span style="color: #000088">$tn</span> <span style="color: #339933">=</span> <span style="color: #cc66cc">1</span><span style="color: #339933">;</span> <span style="color: #000088">$n</span><span style="color: #339933">++;</span> <span style="color: #000088">$noc</span><span style="color: #339933">++;</span>
			<span style="color: #009900">}</span> <span style="color: #b1b100">elseif</span><span style="color: #009900">(</span><span style="color: #cc66cc">194</span> <span style="color: #339933">&</span>lt<span style="color: #339933">;=</span> <span style="color: #000088">$t</span> <span style="color: #339933">&</span>amp<span style="color: #339933">;&</span>amp<span style="color: #339933">;</span> <span style="color: #000088">$t</span> <span style="color: #339933">&</span>lt<span style="color: #339933">;=</span> <span style="color: #cc66cc">223</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
				<span style="color: #000088">$tn</span> <span style="color: #339933">=</span> <span style="color: #cc66cc">2</span><span style="color: #339933">;</span> <span style="color: #000088">$n</span> <span style="color: #339933">+=</span> <span style="color: #cc66cc">2</span><span style="color: #339933">;</span> <span style="color: #000088">$noc</span> <span style="color: #339933">+=</span> <span style="color: #cc66cc">2</span><span style="color: #339933">;</span>
			<span style="color: #009900">}</span> <span style="color: #b1b100">elseif</span><span style="color: #009900">(</span><span style="color: #cc66cc">224</span> <span style="color: #339933">&</span>lt<span style="color: #339933">;=</span> <span style="color: #000088">$t</span> <span style="color: #339933">&</span>amp<span style="color: #339933">;&</span>amp<span style="color: #339933">;</span> <span style="color: #000088">$t</span> <span style="color: #339933">&</span>lt<span style="color: #339933">;=</span> <span style="color: #cc66cc">239</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
				<span style="color: #000088">$tn</span> <span style="color: #339933">=</span> <span style="color: #cc66cc">3</span><span style="color: #339933">;</span> <span style="color: #000088">$n</span> <span style="color: #339933">+=</span> <span style="color: #cc66cc">3</span><span style="color: #339933">;</span> <span style="color: #000088">$noc</span> <span style="color: #339933">+=</span> <span style="color: #cc66cc">2</span><span style="color: #339933">;</span>
			<span style="color: #009900">}</span> <span style="color: #b1b100">elseif</span><span style="color: #009900">(</span><span style="color: #cc66cc">240</span> <span style="color: #339933">&</span>lt<span style="color: #339933">;=</span> <span style="color: #000088">$t</span> <span style="color: #339933">&</span>amp<span style="color: #339933">;&</span>amp<span style="color: #339933">;</span> <span style="color: #000088">$t</span> <span style="color: #339933">&</span>lt<span style="color: #339933">;=</span> <span style="color: #cc66cc">247</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
				<span style="color: #000088">$tn</span> <span style="color: #339933">=</span> <span style="color: #cc66cc">4</span><span style="color: #339933">;</span> <span style="color: #000088">$n</span> <span style="color: #339933">+=</span> <span style="color: #cc66cc">4</span><span style="color: #339933">;</span> <span style="color: #000088">$noc</span> <span style="color: #339933">+=</span> <span style="color: #cc66cc">2</span><span style="color: #339933">;</span>
			<span style="color: #009900">}</span> <span style="color: #b1b100">elseif</span><span style="color: #009900">(</span><span style="color: #cc66cc">248</span> <span style="color: #339933">&</span>lt<span style="color: #339933">;=</span> <span style="color: #000088">$t</span> <span style="color: #339933">&</span>amp<span style="color: #339933">;&</span>amp<span style="color: #339933">;</span> <span style="color: #000088">$t</span> <span style="color: #339933">&</span>lt<span style="color: #339933">;=</span> <span style="color: #cc66cc">251</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
				<span style="color: #000088">$tn</span> <span style="color: #339933">=</span> <span style="color: #cc66cc">5</span><span style="color: #339933">;</span> <span style="color: #000088">$n</span> <span style="color: #339933">+=</span> <span style="color: #cc66cc">5</span><span style="color: #339933">;</span> <span style="color: #000088">$noc</span> <span style="color: #339933">+=</span> <span style="color: #cc66cc">2</span><span style="color: #339933">;</span>
			<span style="color: #009900">}</span> <span style="color: #b1b100">elseif</span><span style="color: #009900">(</span><span style="color: #000088">$t</span> <span style="color: #339933">==</span> <span style="color: #cc66cc">252</span> <span style="color: #339933">||</span> <span style="color: #000088">$t</span> <span style="color: #339933">==</span> <span style="color: #cc66cc">253</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
				<span style="color: #000088">$tn</span> <span style="color: #339933">=</span> <span style="color: #cc66cc">6</span><span style="color: #339933">;</span> <span style="color: #000088">$n</span> <span style="color: #339933">+=</span> <span style="color: #cc66cc">6</span><span style="color: #339933">;</span> <span style="color: #000088">$noc</span> <span style="color: #339933">+=</span> <span style="color: #cc66cc">2</span><span style="color: #339933">;</span>
			<span style="color: #009900">}</span> <span style="color: #b1b100">else</span> <span style="color: #009900">{</span>
				<span style="color: #000088">$n</span><span style="color: #339933">++;</span>
			<span style="color: #009900">}</span>
 
			<span style="color: #b1b100">if</span><span style="color: #009900">(</span><span style="color: #000088">$noc</span> <span style="color: #339933">&</span>gt<span style="color: #339933">;=</span> <span style="color: #000088">$length</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
				<span style="color: #b1b100">break</span><span style="color: #339933">;</span>
			<span style="color: #009900">}</span>
 
		<span style="color: #009900">}</span>
		<span style="color: #b1b100">if</span><span style="color: #009900">(</span><span style="color: #000088">$noc</span> <span style="color: #339933">&</span>gt<span style="color: #339933">;</span> <span style="color: #000088">$length</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
			<span style="color: #000088">$n</span> <span style="color: #339933">-=</span> <span style="color: #000088">$tn</span><span style="color: #339933">;</span>
		<span style="color: #009900">}</span>
 
		<span style="color: #000088">$strcut</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/substr" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">substr</span></a><span style="color: #009900">(</span><span style="color: #000088">$string</span><span style="color: #339933">,</span> <span style="color: #cc66cc">0</span><span style="color: #339933">,</span> <span style="color: #000088">$n</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
 
	<span style="color: #009900">}</span> <span style="color: #b1b100">else</span> <span style="color: #009900">{</span>
 
		<span style="color: #b1b100">for</span><span style="color: #009900">(</span><span style="color: #000088">$i</span> <span style="color: #339933">=</span> <span style="color: #cc66cc">0</span><span style="color: #339933">;</span> <span style="color: #000088">$i</span> <span style="color: #339933">&</span>lt<span style="color: #339933">;</span> <span style="color: #000088">$length</span><span style="color: #339933">;</span> <span style="color: #000088">$i</span><span style="color: #339933">++</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
			<span style="color: #000088">$strcut</span> <span style="color: #339933">.=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/ord" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">ord</span></a><span style="color: #009900">(</span><span style="color: #000088">$string</span><span style="color: #009900">[</span><span style="color: #000088">$i</span><span style="color: #009900">]</span><span style="color: #009900">)</span> <span style="color: #339933">&</span>gt<span style="color: #339933">;</span> <span style="color: #cc66cc">127</span> ? <span style="color: #000088">$string</span><span style="color: #009900">[</span><span style="color: #000088">$i</span><span style="color: #009900">]</span><span style="color: #339933">.</span><span style="color: #000088">$string</span><span style="color: #009900">[</span><span style="color: #339933">++</span><span style="color: #000088">$i</span><span style="color: #009900">]</span> <span style="color: #339933">:</span> <span style="color: #000088">$string</span><span style="color: #009900">[</span><span style="color: #000088">$i</span><span style="color: #009900">]</span><span style="color: #339933">;</span>
		<span style="color: #009900">}</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #666666;font-style: italic">//还原特殊字符串</span>
	<span style="color: #000088">$strcut</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/str_replace" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">str_replace</span></a><span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array</span></a><span style="color: #009900">(</span><span style="color: #000088">$pre</span><span style="color: #339933">.</span><span style="color: #0000ff">'&amp;'</span><span style="color: #339933">.</span><span style="color: #000088">$end</span><span style="color: #339933">,</span> <span style="color: #000088">$pre</span><span style="color: #339933">.</span><span style="color: #0000ff">'"'</span><span style="color: #339933">.</span><span style="color: #000088">$end</span><span style="color: #339933">,</span> <span style="color: #000088">$pre</span><span style="color: #339933">.</span><span style="color: #0000ff">'&lt;'</span><span style="color: #339933">.</span><span style="color: #000088">$end</span><span style="color: #339933">,</span> <span style="color: #000088">$pre</span><span style="color: #339933">.</span><span style="color: #0000ff">'&gt;'</span><span style="color: #339933">.</span><span style="color: #000088">$end</span><span style="color: #009900">)</span><span style="color: #339933">,</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array</span></a><span style="color: #009900">(</span><span style="color: #0000ff">'&amp;'</span><span style="color: #339933">,</span> <span style="color: #0000ff">'"'</span><span style="color: #339933">,</span> <span style="color: #0000ff">'&lt;'</span><span style="color: #339933">,</span> <span style="color: #0000ff">'&gt;'</span><span style="color: #009900">)</span><span style="color: #339933">,</span> <span style="color: #000088">$strcut</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
 
	<span style="color: #666666;font-style: italic">//修复出现特殊字符串截段的问题</span>
	<span style="color: #000088">$pos</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/strrpos" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">strrpos</span></a><span style="color: #009900">(</span><span style="color: #000088">$s</span><span style="color: #339933">,</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/chr" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">chr</span></a><span style="color: #009900">(</span><span style="color: #cc66cc">1</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
	<span style="color: #b1b100">if</span><span style="color: #009900">(</span><span style="color: #000088">$pos</span> <span style="color: #339933">!==</span> <span style="color: #009900;font-weight: bold">false</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
		<span style="color: #000088">$strcut</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/substr" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">substr</span></a><span style="color: #009900">(</span><span style="color: #000088">$s</span><span style="color: #339933">,</span><span style="color: #cc66cc">0</span><span style="color: #339933">,</span><span style="color: #000088">$pos</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
	<span style="color: #b1b100">return</span> <span style="color: #000088">$strcut</span><span style="color: #339933">.</span><span style="color: #000088">$dot</span><span style="color: #339933">;</span>
<span style="color: #009900">}</span>