Home php教程 php手册 截取函数,中文一个,英文两个算一个

截取函数,中文一个,英文两个算一个

Jun 07, 2016 am 11:43 AM

截取函数,中文一个,英文两个算一个
共享一个今天写的截取字符串函数,跟官方的不一样,用了你就知道/**<br> * 截取函数,中文一个,英文两个算一个<br> */<br> function mbsubstr($str, $start=0, $length, $suffix=true, $charset="utf-8") {<br>     $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";<br>     $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";<br>     $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";<br>     $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";<br>     preg_match_all($re[$charset], $str, $match);<br>     <br>     $strArr = $match[0];<br>     $str = array_values(tempStrArr($strArr));<br>     $strArrCount = count($str);<br>     if($strArrCount == $length+1){<br>         $length += 1;<br>     }<br>     <br>     $slice = implode('',array_slice($str,$start,$length));<br>     if($strArrCount &gt; $length){<br>         return $suffix ? $slice.'...' : $slice;<br>     }else{<br>         return $slice;<br>     }<br> }<br> function tempStrArr($strArr){<br>     $e = range('a','z');<br>     $i = count($strArr);<br>     foreach($strArr as $k=&gt;$v){<br>         if(in_array(strtolower($v), $e)){<br>             if(in_array(strtolower($strArr[$k+1]), $e)){<br>                 $strArr[$k] = $v.$strArr[$k+1];<br>                 unset($strArr[$k+1]);<br>                 break;<br>             }<br>         }elseif($v &gt; 0 &amp;&amp; $v  0 &amp;&amp; $strArr[$k+1]              $strArr[$k] = $v.$strArr[$k+1];<br>             unset($strArr[$k+1]);<br>             break;<br>         }<br>         $i--;<br>     }<br>     if($i &gt; 0){<br>         $strArr = tempStrArr($strArr);<br>     }<br>     <br>     return $strArr;<br> }共享这个代码。我总觉得哪里不对劲,有更好的思路的大神求思路



更多函数参见 http://chajiandaquan.com/function.html

AD:真正免费,域名+虚机+企业邮箱=0元

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Learn about introductory code examples for Python programming Learn about introductory code examples for Python programming Jan 04, 2024 am 10:50 AM

Learn about introductory code examples for Python programming

PHP variables in action: 10 real-life examples of use PHP variables in action: 10 real-life examples of use Feb 19, 2024 pm 03:00 PM

PHP variables in action: 10 real-life examples of use

From beginner to proficient: Code implementation of commonly used data structures in Go language From beginner to proficient: Code implementation of commonly used data structures in Go language Mar 04, 2024 pm 03:09 PM

From beginner to proficient: Code implementation of commonly used data structures in Go language

How to use PHP to write inventory management function code in the inventory management system How to use PHP to write inventory management function code in the inventory management system Aug 06, 2023 pm 04:49 PM

How to use PHP to write inventory management function code in the inventory management system

Java implements simple bubble sort code Java implements simple bubble sort code Jan 30, 2024 am 09:34 AM

Java implements simple bubble sort code

Go language programming examples: code examples in web development Go language programming examples: code examples in web development Mar 04, 2024 pm 04:54 PM

Go language programming examples: code examples in web development

Huawei Cloud Edge Computing Interconnection Guide: Java code examples to quickly implement interfaces Huawei Cloud Edge Computing Interconnection Guide: Java code examples to quickly implement interfaces Jul 05, 2023 pm 09:57 PM

Huawei Cloud Edge Computing Interconnection Guide: Java code examples to quickly implement interfaces

Guidance and Examples: Learn to implement the selection sort algorithm in Java Guidance and Examples: Learn to implement the selection sort algorithm in Java Feb 18, 2024 am 10:52 AM

Guidance and Examples: Learn to implement the selection sort algorithm in Java

See all articles