以前也写过一个PHP文章摘要生成方法(函数), 原文地址: http://www.chhua.com/web-note628 ,不过,不怎么好用,也出现乱码,现在再发布一个,这个函数是在某开源系统上拆下来了,希望对大家用用,在使用的时候,得先把要生成摘要的内容strip_tags()一下,当然,你也可以把strip_tags()直接添加到函数中,我没有搞,自己添加吧。下面是函数:

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
 
<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> <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">'&quot;'</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">'&'</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">'<'</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: #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> <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> <span style="color: #000088">$t</span> <span style="color: #339933">&&</span> <span style="color: #000088">$t</span> <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> <span style="color: #000088">$t</span> <span style="color: #339933">&&</span> <span style="color: #000088">$t</span> <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> <span style="color: #000088">$t</span> <span style="color: #339933">&&</span> <span style="color: #000088">$t</span> <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> <span style="color: #000088">$t</span> <span style="color: #339933">&&</span> <span style="color: #000088">$t</span> <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> <span style="color: #000088">$t</span> <span style="color: #339933">&&</span> <span style="color: #000088">$t</span> <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> <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> <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> <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> <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">'&'</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">'<'</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: #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">'&quot;'</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>