{"id":161,"date":"2013-06-07T17:45:05","date_gmt":"2013-06-07T09:45:05","guid":{"rendered":"http:\/\/www.zyuns.com\/?p=161"},"modified":"2015-02-15T20:50:51","modified_gmt":"2015-02-15T12:50:51","slug":"bi-jiao-discuz-he-ecshop-de-jie-qu-zi-fu-chuan-han-shu-php-ban","status":"publish","type":"post","link":"https:\/\/www.siediyer.cn\/?p=161","title":{"rendered":"\u6bd4\u8f83discuz\u548cecshop\u7684\u622a\u53d6\u5b57\u7b26\u4e32\u51fd\u6570php\u7248"},"content":{"rendered":"<p>\u4e0b\u9762\u5148\u7ed9\u51fa\u4e24\u4e2a\u7248\u672c\u51fd\u6570\u7684\u6e90\u4ee3\u7801\u4ee5\u53ca\u7b80\u5355\u6d4b\u8bd5\uff0c\u6700\u540e\u6211\u4f1a\u7ed9\u51fa\u4e00\u4e2a\u5b9e\u7528\u6027\u66f4\u5f3a\u7684\u5b57\u7b26\u4e32\u622a\u53d6\u51fd\u6570\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a\u8fd9\u91cc\u8ba8\u8bba\u7684\u5b57\u7b26\u4e32\u622a\u53d6\u95ee\u9898\u90fd\u662f\u9488\u5bf9UTF-8\u7f16\u7801\u7684\u4e2d\u6587\u5b57\u7b26\u4e32\u3002<br \/>\n<strong>discuz\u7248\u672c<\/strong><\/p>\n<pre class=\"wrap:true lang:default decode:true\">\/**\n* [discuz] \u57fa\u4e8ePHP\u6ca1\u6709\u5b89\u88c5 mb_substr \u7b49\u6269\u5c55\u622a\u53d6\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u622a\u53d6\u4e2d\u6587\u5b57\u5219\u63092\u4e2a\u5b57\u7b26\u8ba1\u7b97\n* @param $string \u8981\u622a\u53d6\u7684\u5b57\u7b26\u4e32\n* @param $length \u8981\u622a\u53d6\u7684\u5b57\u7b26\u6570\n* @param $dot \u66ff\u6362\u622a\u6389\u90e8\u5206\u7684\u7ed3\u5c3e\u5b57\u7b26\u4e32\n* @return \u8fd4\u56de\u622a\u53d6\u540e\u7684\u5b57\u7b26\u4e32\n*\/\nfunction cutstr($string, $length, $dot = '...') {\n\/\/ \u5982\u679c\u5b57\u7b26\u4e32\u5c0f\u4e8e\u8981\u622a\u53d6\u7684\u957f\u5ea6\u5219\u76f4\u63a5\u8fd4\u56de\n\/\/ \u6b64\u5904\u4f7f\u7528strlen\u83b7\u53d6\u5b57\u7b26\u4e32\u957f\u5ea6\u6709\u5f88\u5927\u7684\u5f0a\u75c5\uff0c\u6bd4\u5982\u5bf9\u5b57\u7b26\u4e32\u201c\u65b0\u5e74\u5feb\u4e50\u201d\u8981\u622a\u53d64\u4e2a\u4e2d\u6587\u5b57\u7b26\uff0c\n\/\/ \u90a3\u4e48\u5fc5\u987b\u77e5\u9053\u8fd94\u4e2a\u4e2d\u6587\u5b57\u7b26\u7684\u5b57\u8282\u6570\uff0c\u5426\u5219\u8fd4\u56de\u7684\u5b57\u7b26\u4e32\u53ef\u80fd\u4f1a\u662f\u201c\u65b0\u5e74\u5feb\u4e50...\u201d\nif (strlen($string) &lt;= $length) {\nreturn $string;\n}\n\/\/ \u8f6c\u6362\u539f\u5b57\u7b26\u4e32\u4e2dhtmlspecialchars\n$pre = chr(1);\n$end = chr(1);\n$string = str_replace ( array ('&amp;', '\"', '&lt;', '&gt;' ), array ($pre . '&amp;' . $end, $pre . '\"' . $end, $pre . '&lt;' . $end, $pre . '&gt;' . $end ), $string );\n$strcut = ''; \/\/ \u521d\u59cb\u5316\u8fd4\u56de\u503c\n\/\/ \u5982\u679c\u662futf-8\u7f16\u7801(\u8fd9\u4e2a\u5224\u65ad\u6709\u70b9\u4e0d\u5168,\u6709\u53ef\u80fd\u662futf8)\nif (strtolower ( CHARSET ) == 'utf-8') {\n\/\/ \u521d\u59cb\u8fde\u7eed\u5faa\u73af\u6307\u9488$n,\u6700\u540e\u4e00\u4e2a\u5b57\u4f4d\u6570$tn,\u622a\u53d6\u7684\u5b57\u7b26\u6570$noc\n$n = $tn = $noc = 0;\nwhile ( $n &lt; strlen ( $string ) ) {\n$t = ord ( $string [$n] );\nif ($t == 9 || $t == 10 || (32 &lt;= $t &amp;&amp; $t &lt;= 126)) {\n\/\/ \u5982\u679c\u662f\u82f1\u8bed\u534a\u89d2\u7b26\u53f7\u7b49,$n\u6307\u9488\u540e\u79fb1\u4f4d,$tn\u6700\u540e\u5b57\u662f1\u4f4d\n$tn = 1;\n$n++;\n$noc++;\n} elseif (194 &lt;= $t &amp;&amp; $t &lt;= 223) {\n\/\/ \u5982\u679c\u662f\u4e8c\u5b57\u8282\u5b57\u7b26$n\u6307\u9488\u540e\u79fb2\u4f4d,$tn\u6700\u540e\u5b57\u662f2\u4f4d\n$tn = 2;\n$n += 2;\n$noc += 2;\n} elseif (224 &lt;= $t &amp;&amp; $t &lt;= 239) {\n\/\/ \u5982\u679c\u662f\u4e09\u5b57\u8282(\u53ef\u4ee5\u7406\u89e3\u4e3a\u4e2d\u5b57\u8bcd),$n\u540e\u79fb3\u4f4d,$tn\u6700\u540e\u5b57\u662f3\u4f4d\n$tn = 3;\n$n += 3;\n$noc += 2;\n} elseif (240 &lt;= $t &amp;&amp; $t &lt;= 247) {\n$tn = 4;\n$n += 4;\n$noc += 2;\n} elseif (248 &lt;= $t &amp;&amp; $t &lt;= 251) {\n$tn = 5;\n$n += 5;\n$noc += 2;\n} elseif ($t == 252 || $t == 253) {\n$tn = 6;\n$n += 6;\n$noc += 2;\n} else {\n$n++;\n}\n\/\/ \u8d85\u8fc7\u4e86\u8981\u53d6\u7684\u6570\u5c31\u8df3\u51fa\u8fde\u7eed\u5faa\u73af\nif ($noc &gt;= $length) {\nbreak;\n}\n}\n\/\/ \u8fd9\u4e2a\u5730\u65b9\u662f\u628a\u6700\u540e\u4e00\u4e2a\u5b57\u53bb\u6389,\u4ee5\u5907\u52a0$dot\nif ($noc &gt; $length) {\n$n -= $tn;\n}\n$strcut = substr ( $string, 0, $n );\n} else {\n\/\/ \u5e76\u975eutf-8\u7f16\u7801\u7684\u5168\u89d2\u5c31\u540e\u79fb2\u4f4d\nfor ($i = 0; $i &lt; $length; $i ++) {\n$strcut .= ord ( $string [$i] ) &gt; 127 ? $string [$i] . $string [++ $i] : $string [$i];\n}\n}\n\/\/ \u518d\u8fd8\u539f\u6700\u521d\u7684htmlspecialchars\n$strcut = str_replace( array ($pre . '&amp;' . $end, $pre . '\"' . $end, $pre . '&lt;' . $end, $pre . '&gt;' . $end ), array ('&amp;', '\"', '&lt;', '&gt;' ), $strcut );\n$pos = strrpos ( $strcut, chr ( 1 ) );\nif ($pos !== false) {\n$strcut = substr ( $strcut, 0, $pos );\n}\nreturn $strcut . $dot; \/\/ \u6700\u540e\u628a\u622a\u53d6\u52a0\u4e0a$dot\u8f93\u51fa\n}<\/pre>\n<p>discuz\u7248\u672c\u7684\u6700\u5927\u7f3a\u9677\u5728\u4e8e\u4f7f\u7528 strlen \u83b7\u53d6\u539f\u59cb\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u5e76\u7528\u6765\u548c\u4f20\u5165\u7684\u8981\u622a\u53d6\u957f\u5ea6\u53c2\u6570\uff08\u5b57\u8282\u6570\uff09\u8fdb\u884c\u6bd4\u8f83\uff0c\u7531\u4e8eUTF-8\u7684\u4e2d\u6587\u5b57\u7b26\u7684\u5b57\u8282\u6570\u662f\u4e0d\u56fa\u5b9a\u7684\uff0c\u6240\u4ee5\u5c31\u4f1a\u9762\u4e34\u8fd9\u6837\u7684\u7a98\u5883\uff1a\u5982\u679c\u8981 \u622a\u53d64\u4e2a\u4e2d\u6587\u5b57\u7b26\u5e94\u8be5\u6307\u5b9a\u591a\u5927\u7684\u622a\u53d6\u957f\u5ea6\u5462\uff1f8\u5b57\u8282\u8fd8\u662f12\u5b57\u8282\u5462\uff1f\u3002\u3002\u3002\u8fd9\u662f\u65e0\u6cd5\u9884\u8ba1\u7684\uff0c\u4e5f\u6b63\u662f\u56e0\u4e3a\u8fd9\u4e2a\u95ee\u9898discuz\u7684cutstr\u5b9e\u9645\u662f\u6709bug \u7684\uff0c\u901a\u8fc7\u4e0b\u9762\u7684\u6d4b\u8bd5\u7ed3\u679c\u80fd\u770b\u51fa\uff1a<\/p>\n<pre class=\"wrap:true lang:default decode:true \">$str1 = \"\u6b32\u7a77\u5343\u91cc\u76ee\";\necho my_cutstr($str1, 10, \"...\").\"n\"; \/\/ \u8f93\u51fa\uff1a\u6b32\u7a77\u5343\u91cc\u76ee... [\u8fd9\u662f\u4e00\u4e2abug\uff0c\u60f3\u60f3\u662f\u4ec0\u4e48\u539f\u56e0\u5bfc\u81f4\uff1f]\necho my_cutstr($str1, 15, \"...\").\"n\"; \/\/ \u8f93\u51fa\uff1a\u6b32\u7a77\u5343\u91cc\u76ee<\/pre>\n<p>\u5bfc\u81f4\u4e0a\u8ff0bug\u7684\u539f\u56e0\u5728\u4e0ecutstr\u51fd\u6570\u5728\u622a\u53d6\u5b57\u7b26\u7684\u65f6\u5019\u662f\u5c06\u4e00\u4e2a\u4e2d\u6587\u5b57\u63092\u4e2a\u5b57\u7b26\u7b97\uff0c\u90a3\u4e485\u4e2a\u4e2d\u6587\u5b57\u5c31\u662f10\u5b57\u7b26\uff0c\u800c\u539f\u59cb\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f15\u5b57\u8282\uff0c\u6240 \u4ee5cutstr\u8ba4\u4e3a\u201c\u6210\u529f\u5730\u201d\u4ece15\u5b57\u7b26\u7684\u4e32\u4e0a\u622a\u53d6\u4e8610\u4e2a\u5b57\u7b26\uff0c\u7136\u540e\u52a0\u4e0a\u4e86\u201c\u5c3e\u5df4\u201d\u3002\u8981\u89e3\u51b3\u8fd9\u4e2abug\u53ea\u8981\u5728\u5224\u65ad\u4e00\u4e0b\u8fd4\u56de\u7684\u5b50\u4e32\u662f\u5426\u548c\u539f\u59cb\u4e32\u76f8\u540c\uff0c\u5982\u679c \u76f8\u540c\u5c31\u4e0d\u52a0\u201c\u5c3e\u5df4\u201d\u3002<br \/>\n<strong>ecshop\u7248<\/strong><\/p>\n<pre class=\"wrap:true lang:default decode:true \">\/**\n* [ecshop] \u57fa\u4e8ePHP\u7684 mb_substr\uff0ciconv_substr \u8fd9\u4e24\u4e2a\u6269\u5c55\u6765\u622a\u53d6\u5b57\u7b26\u4e32\uff0c\u4e2d\u6587\u5b57\u7b26\u90fd\u662f\u63091\u4e2a\u5b57\u7b26\u957f\u5ea6\u8ba1\u7b97\uff1b\n* \u8be5\u51fd\u6570\u4ec5\u9002\u7528\u4e8eutf-8\u7f16\u7801\u7684\u4e2d\u6587\u5b57\u7b26\u4e32\u3002\n*\n* @param $str \u539f\u59cb\u5b57\u7b26\u4e32\n* @param $length \u622a\u53d6\u7684\u5b57\u7b26\u6570\n* @param $append \u66ff\u6362\u622a\u6389\u90e8\u5206\u7684\u7ed3\u5c3e\u5b57\u7b26\u4e32\n* @return \u8fd4\u56de\u622a\u53d6\u540e\u7684\u5b57\u7b26\u4e32\n*\/\nfunction sub_str($str, $length = 0, $append = '...') {\n$str = trim($str);\n$strlength = strlen($str);\nif ($length == 0 || $length &gt;= $strlength) {\nreturn $str;\n} elseif ($length &lt; 0) {\n$length = $strlength + $length;\nif ($length &lt; 0) {\n$length = $strlength;\n}\n}\nif ( function_exists('mb_substr') ) {\n$newstr = mb_substr($str, 0, $length, 'utf-8');\n} elseif ( function_exists('iconv_substr') ) {\n$newstr = iconv_substr($str, 0, $length, 'utf-8');\n} else {\n\/\/$newstr = trim_right(substr($str, 0, $length));\n$newstr = substr($str, 0, $length);\n}\nif ($append &amp;&amp; $str != $newstr) {\n$newstr .= $append;\n}\nreturn $newstr;\n}<\/pre>\n<p>ecshop\u7248\u7684\u7279\u70b9\u548c\u7f3a\u70b9\u90fd\u5728\u4e8e\u5c06\u4e2d\u6587\u5b57\u7b26\u7b97\u4f5c\u4e00\u4e2a\u5b57\u7b26\uff0c\u5982\u679c\u539f\u59cb\u5b57\u7b26\u4e32\u4e2d\u4e0d\u542b\u4e2d\u6587\uff0c\u6bd4\u5982\uff1aabcd1234\uff0c\u5982\u679c\u672c\u610f\u662f\u8981\u622a\u53d64\u4e2a\u4e2d\u6587\u5b57\u7b26\u6216\u80058\u4e2a\u82f1\u6587\u5b57\u7b26\uff0c\u90a3\u4e48\u4f7f\u7528ecshop\u7684\u7248\u672c\u5c31\u5f97\u4e0d\u5230\u671f\u671b\u7684\u7ed3\u679c\uff0c\u8fd4\u56de\u503c\u7684\u662f\uff1aabcd\u3002\u4e0b\u9762\u662f\u7b80\u5355\u7684\u6d4b\u8bd5\u7ed3\u679c\uff1a<\/p>\n<pre class=\"wrap:true lang:default decode:true \">$str1 = \"\u767d\u65e5\u4f9d\u5c71\u5c3d\uff0c\u9ec4\u6cb3\u5165\u6d77\u6d41\";\necho $str1.\"n\";\necho my_sub_str($str1, 4, \"...\").\"n\"; \/\/ \u8f93\u51fa\uff1a\u767d\u65e5\u4f9d\u5c71...\n$str2 = \"\u767d1\u65e52\u4f9d3\u5c714\";\necho $str2.\"n\";\necho my_sub_str($str2, 4, \"...\").\"n\"; \/\/ \u8f93\u51fa\uff1a\u767d1\u65e52...<\/pre>\n<p><strong>\u4f18\u5316\u7248<br \/>\n<\/strong>\u622a\u53d6\u4e2d\u6587\u5b57\u7b26\u4e32\u7684\u5927\u90e8\u5206\u5e94\u7528\u573a\u666f\u662f\u201c\u539f\u59cb\u5b57\u7b26\u4e32\u53ef\u4ee5\u662f\u4e2d\u6587\u3001\u82f1\u6587\u3001\u6570\u5b57\u6df7\u6742\u7684\uff0c\u4e2d\u6587\u5b57\u63092\u4e2a\u5b57\u7b26\u7b97\uff0c\u82f1\u6587\u6570\u5b57\u63091\u4e2a\u5b57\u7b26\u7b97\u201d\uff0c\u9488\u5bf9\u8fd9\u4e2a\u9700\u6c42\u4e0b\u9762\u7ed9\u51fa\u4e00\u4e2a\u5b9e\u73b0\u7248\u672c\uff1a<\/p>\n<pre class=\"wrap:true lang:default decode:true \">\/**\n* \u5b57\u7b26\u4e32\u622a\u53d6\uff0c\u4e2d\u6587\u5b57\u7b26\u63092\u4e2a\u5b57\u7b26\u8ba1\u7b97\uff0c\u540c\u65f6\u652f\u6301GBK\u548cUTF-8\u7f16\u7801\n* @param $string \u8981\u622a\u53d6\u7684\u5b57\u7b26\u4e32\n* @param $length \u8981\u622a\u53d6\u7684\u5b57\u7b26\u6570\n* @param $append \u6dfb\u52a0\u5230\u5b50\u4e32\u540e\u7684\u5c3e\u5df4\n* @return \u8fd4\u56de\u622a\u53d6\u540e\u7684\u5b57\u7b26\u4e32\n*\/\nfunction substring($string, $length, $append = false) {\nif ( $length &lt;= 0 ) {\nreturn '';\n}\n\/\/ \u68c0\u6d4b\u539f\u59cb\u5b57\u7b26\u4e32\u662f\u5426\u4e3aUTF-8\u7f16\u7801\n$is_utf8 = false;\n$str1 = @iconv(\"UTF-8\", \"GBK\", $string);\n$str2 = @iconv(\"GBK\", \"UTF-8\", $str1);\nif ( $string == $str2 ) {\n$is_utf8 = true;\n\/\/ \u5982\u679c\u662fUTF-8\u7f16\u7801\uff0c\u5219\u4f7f\u7528GBK\u7f16\u7801\u7684\n$string = $str1;\n}\n$newstr = '';\nfor ($i = 0; $i &lt; $length; $i ++) {\n$newstr .= ord ($string[$i]) &gt; 127 ? $string[$i] . $string[++$i] : $string[$i];\n}\nif ( $is_utf8 ) {\n$newstr = @iconv(\"GBK\", \"UTF-8\", $newstr);\n}\nif ($append &amp;&amp; $newstr != $string) {\n$newstr .= $append;\n}\nreturn $newstr;\n}<\/pre>\n<p>\u6d4b\u8bd5\u7ed3\u679c\u89c1\u4e0b\uff08GBK\u548cUTF-8\u7684\u7ed3\u679c\u4e00\u81f4\uff09:<\/p>\n<pre class=\"wrap:true lang:default decode:true \">$str1 = \"\u767d\u65e5\u4f9d\u5c71\u5c3d\uff0c\u9ec4\u6cb3\u5165\u6d77\u6d41\";\necho substring($str1, 4, \"...\").\"n\"; \/\/ \u8f93\u51fa\uff1a\u767d\u65e5...\necho substring($str1, 5, \"...\").\"n\"; \/\/ \u8f93\u51fa\uff1a\u767d\u65e5\u4f9d...\n$str2 = \"12\u767d34\u65e556\u4f9d78\u5c71\";\necho substring($str2, 4, \"...\").\"n\"; \/\/ \u8f93\u51fa\uff1a12\u767d...\necho substring($str2, 5, \"...\").\"n\"; \/\/ \u8f93\u51fa\uff1a12\u767d3...<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u5148\u7ed9\u51fa\u4e24\u4e2a\u7248\u672c\u51fd\u6570\u7684\u6e90\u4ee3\u7801\u4ee5\u53ca\u7b80\u5355\u6d4b\u8bd5\uff0c\u6700\u540e\u6211\u4f1a\u7ed9\u51fa\u4e00\u4e2a\u5b9e\u7528\u6027\u66f4\u5f3a\u7684\u5b57\u7b26\u4e32\u622a\u53d6\u51fd\u6570\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a\u8fd9\u91cc\u8ba8\u8bba [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-161","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=161"}],"version-history":[{"count":1,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/161\/revisions"}],"predecessor-version":[{"id":444,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/161\/revisions\/444"}],"wp:attachment":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}