求中文,英文utf8下统一的计数和截取方法
做网页,一行只有37个英文字符大小的地方,要判断是不是长了,不是就全显示,是就截取到17个中文字符或34个英文字符长的位置再加上三个点“...”做省略号,因为不能判断用户到时候用哪个国家的文字(没准还用俄语),所以需要一个统一的算法,不知道哪位大侠有办法,谢谢。
------解决思路----------------------
ThinkPHP框架中有一个截取函数比较不错
/**<br /> +----------------------------------------------------------<br /> * 字符串截取,支持中文和其他编码<br /> +----------------------------------------------------------<br /> * @static<br /> * @access public<br /> +----------------------------------------------------------<br /> * @param string $str 需要转换的字符串<br /> * @param string $start 开始位置<br /> * @param string $length 截取长度<br /> * @param string $charset 编码格式<br /> * @param string $suffix 截断显示字符<br /> +----------------------------------------------------------<br /> * @return string<br /> +----------------------------------------------------------<br /> */<br />function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)<br />{<br /> if(function_exists("mb_substr"))<br /> {<br /> if ($suffix && strlen($str)>$length)<br /> return mb_substr($str, $start, $length, $charset)."...";<br /> else<br /> return mb_substr($str, $start, $length, $charset);<br /> }<br /> elseif(function_exists('iconv_substr'))<br /> {<br /> if ($suffix && strlen($str)>$length)<br /> return iconv_substr($str,$start,$length,$charset)."...";<br /> else<br /> return iconv_substr($str,$start,$length,$charset);<br /> }<br /><br /> $re['utf-8'] = "/[\x01-\x7f]<br><font color='#FF8000'>------解决思路----------------------</font><br>[\xc2-\xdf][\x80-\xbf]<br><font color='#FF8000'>------解决思路----------------------</font><br>[\xe0-\xef][\x80-\xbf]{2}<br><font color='#FF8000'>------解决思路----------------------</font><br>[\xf0-\xff][\x80-\xbf]{3}/";<br /> $re['gb2312'] = "/[\x01-\x7f]<br><font color='#FF8000'>------解决思路----------------------</font><br>[\xb0-\xf7][\xa0-\xfe]/";<br /> $re['gbk'] = "/[\x01-\x7f]<br><font color='#FF8000'>------解决思路----------------------</font><br>[\x81-\xfe][\x40-\xfe]/";<br /> $re['big5'] = "/[\x01-\x7f]<br><font color='#FF8000'>------解决思路----------------------</font><br>[\x81-\xfe]([\x40-\x7e]<br><font color='#FF8000'>------解决思路----------------------</font><br>\xa1-\xfe])/";<br /> preg_match_all($re[$charset], $str, $match);<br /> $slice = join("",array_slice($match[0], $start, $length));<br /> if($suffix)<br /> return $slice."…";<br /> return $slice;<br />}
------解决思路----------------------
在浏览器中,一个汉字占两个西文字符的位置,这个说法是针对等宽字体而言的
在此前提下针对 utf-8 编码的中西文混合字符串的显示宽度(西文为1、中文为2)有如下计算公式
(按字节计的长度 - 按字计的长度) / 2 + 按字计的长度
比如 aa中国
按字节计的长度为 8
按字计的长度为 4
带入得 (8-4)/2+4 = 6
即 6 个显示单位
如果你需要先指定显示长度,就需要做循环试探了
其实 css 已经提供了更好的解决方案
<style><br />body{<br /> background-color:#f4f4f4;<br /> font-size:12px;<br />}<br />div.test{<br /> width:200px;<br /> height:60px;<br /> border:1px solid red;<br /> border-top:4px solid red;<br /> padding:10px;<br /> overflow:hidden;<br /> text-overflow:ellipsis;<br /> white-space:nowrap;<br />}<br /></style><br /><div class="test">DIV+CSS模板、后台模板、图片图标下载,CSS代码实例、CSS导航菜单、CSS图表<br /></div><br />

------解决思路----------------------
text-overflow:ellipsis; 貌似IE only.

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Atom editor mac version download
The most popular open source editor

Dreamweaver CS6
Visual web development tools
