转载请注明来源:php字符串截取函数,支持中文 utf-8截取
- <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="content-type" content="text/html; charset=utf-8" />
- <title>environment</title>
- </head>
- <?php
- $str = "中12hb千钧一发lap";
- echo mb_substr($str,0,5,'utf-8');echo '<br>';
- echo mb_substr($str,5,3,'utf-8');echo '<br>';
- echo mb_substr($str,8,4,'utf-8');
- ?>
- <body>
- </body>
- </html>
- <?php
- function str_wrap1($str,$elen=30)
- {
- $tlen = mb_strlen($str,"utf8"); //共有多少字
- //$elen = 8; //每行字符串长度8个字符、4个汉字
- $dlen = 0; //每行显示长度
- $str_wrap = '';
- for($i=0;$i<$tlen;$i++)
- {
- $tmpchar = mb_substr($str,$i,1,"utf8");
- if(strlen($tmpchar) == 3)
- $charlen = 2;
- else
- $charlen = 1;
- if( $dlen < $elen-1 )
- {
- $dlen += $charlen;
- $str_wrap .= $tmpchar;
- }//开源软件:phpfensi.com
- else
- {
- $str_wrap .= "<br>".$tmpchar;
- $dlen = $charlen;
- }
- }
- return $str_wrap;
- }
- echo str_wrap1($str,5);
- ?>
哈尔滨品用软件有限公司致力于为哈尔滨的中小企业制作大气、美观的优秀网站,并且能够搭建符合百度排名规范的网站基底,使您的网站无需额外费用,即可稳步提升排名至首页。欢迎体验最佳的哈尔滨网站建设。