Thinkphp method for intercepting Chinese strings _ PHP Tutorial

Source: Internet
Author: User
Thinkphp intercepts Chinese strings. ThinkPHP3.1.3 does not seem to have a built-in method for intercepting Chinese strings. I haven't found it for a long time. The author adds a function to intercept Chinese strings. the specific code is as follows, if ThinkPHP 3.1.3 is required, it seems that there is no built-in method to intercept Chinese strings. it has not been found for a long time. The author adds a function to intercept Chinese strings. the specific code is as follows, for more information, see.

Add the following code to the Common directory in the project directory. php files, such as www/Common/common. php file, of course, you can also directly add it to the Common/common of thinkphp. in the php file, all projects can be used.

Function truncate_cn ($ string, $ length = 0, $ ellipsis = '... ', $ Start = 0) {$ string = strip_tags ($ string); $ string = preg_replace ('/\ n/is ', '', $ string ); // $ string = preg_replace ('/|/is', ', $ string); // clear the spaces in the string $ string = preg_replace (' // is ', '', $ string ); preg_match_all ("/[\ x01-\ x7f] | [\ xc2-\ xdf] [\ x80-\ xbf] | \ xe0 [\ xa0-\ xbf] [\ x80- \ xbf] | [\ xe1-\ xef] [\ x80-\ xbf] [\ x80-\ xbf] | \ xf0 [\ x90-\ xbf] [\ x80 -\ xbf] [\ x80-\ xbf] | [\ xf1-\ xf7] [\ x80-\ xbf] [\ x80-\ xbf] [\ x80-\ xbf]/", $ string, $ string); if (is_a Rray ($ string )&&! Empty ($ string [0]) {$ string = implode ('', $ string [0]); if (strlen ($ string) <$ start + 1) {return '';} preg_match_all ("/. /su ", $ string, $ ar); $ string2 =''; $ tstr = ''; // www. phpernote. comfor ($ I = 0; isset ($ ar [0] [$ I]); $ I ++) {if (strlen ($ tstr) <$ start) {$ tstr. = $ ar [0] [$ I];} else {if (strlen ($ string2) <$ length + strlen ($ ar [0] [$ I]) {$ string2. = $ ar [0] [$ I];} else {break ;}} return $ string ==$ string2? $ String2: $ string2. $ ellipsis;} else {$ string = '';} return $ string ;}

The thinkphp template is used as follows:

{$info.subject|truncate_cn=40,'',0}

It means to intercept the $ info ['subobject'] string. the string with a length of 40 is truncated starting from 0th. if the length of the truncated string is smaller than the length of the original string, nothing is displayed, the default value is... in fact, the following two parameters are optional.

Articles you may be interested in
  • Summary of the string truncation function implemented by javascript (including Introduction to the use of Js to intercept Chinese characters)
  • The last record of the volist loop in the thinkphp Template
  • Php performance optimization: using isset () to determine the string length is faster than strlen ()
  • Js restrictions: only English letters and numbers can be entered. Chinese characters and other special characters cannot be entered.
  • Thinkphp automatic verification and automatic filling are invalid solutions
  • Php extracts numbers from strings
  • How to remove the index. php string from the website url developed by codeIgniter
  • Php determines whether the string is full of English, pure Chinese, and a combination of Chinese and English

Http://www.bkjia.com/PHPjc/764109.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/764109.htmlTechArticleThinkPHP 3.1.3 seems to have no built-in method of intercepting Chinese strings, looking for a long time did not find, the author himself added a function of intercepting Chinese strings, the specific code is as follows, there is a need...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.