Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the php interception string functions that support Chinese and other encodings

Shulou Source: shulou.com Published: 2022-06-03 07:48:58 09月11日 Update

The main content of this article is to explain "what are the php intercept string functions that support Chinese and other codes", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the php intercept string functions that support Chinese and other codes.

A simple string interception method that supports Chinese and other encodings. Just pass in the corresponding parameters and call them directly.

The copy code is as follows:

/ * *

* string interception, which supports Chinese and other encodings

* @ static

* @ access public

* @ param string $str string to be converted

* @ param string $start start position

* @ param string $length intercept length

* @ param string $charset encoding format

* @ param string $suffix truncate display characters

* @ return string

, /

Function msubstr ($str, $start=0, $length, $charset= "utf-8", $suffix=true) {

If (function_exists ("mb_substr"))

$slice = mb_substr ($str, $start, $length, $charset)

Elseif (function_exists ('iconv_substr')) {

$slice = iconv_substr ($str,$start,$length,$charset)

If (false = = $slice) {

$slice =''

}

} else {

$re ['utf-8'] = "/ [\ x01 -\ x7f] | [\ xc2-\ xdf] [\ x80 -\ xbf] | [\ xe0-\ xef] [\ x80 -\ xbf] {2} | [\ xf0-\ xff] [\ x80 -\ xbf] {3} /"

$re ['gb2312'] = "/ [\ x01 -\ x7f] | [\ xb0-\ xf7] [\ xa0-\ xfe] /"

$re ['gbk'] = "/ [\ x01 -\ x7f] | [\ x81 -\ xfe] [\ x40 -\ xfe] /"

$re ['big5'] = "/ [\ x01 -\ x7f] | [\ x81 -\ xfe] ([\ x40 -\ x7e] |\ xa1-\ xfe]) /"

Preg_match_all ($re [$charset], $str, $match)

$slice = join ("", array_slice ($match [0], $start, $length))

}

Return $suffix? $slice.'...': $slice

}

At this point, I believe you have a deeper understanding of "what are the php intercept string functions that support Chinese and other codes?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Characters strings coding Chinese support functions code content methods learning practical deeper location interest parameters practicality practical simple operation more friends Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Linux Redmi Huawei macOS