What are the methods for intercepting php strings?

青灯夜游
Release: 2023-03-10 11:02:02
Original
4210 people have browsed it

Method: 1. Use the "substr (string, starting position, interception length)" statement; 2. Use the "mb_substr (string, starting position, interception length, encoding)" statement; 3. Use " mb_strcut (string, starting position, interception length, encoding)" statement; 4. Use the "$str[character position]" statement.

What are the methods for intercepting php strings?

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

substr(string, starting position , intercept length)

If the intercepted string is multiple bytes, garbled characters will appear, such as Chinese, suitable for intercepting letters and numbers

$str = 'hello';
echo substr($str,1,2);//输出el
Copy after login

mb_substr( String, starting position, interception length, character encoding)

Suitable for intercepting Chinese, segmented by characters

echo mb_substr('这个真的很nice',0,3,'utf-8'); //输出这个真
Copy after login

mb_strcut(String, starting position, interception Length, character encoding)

Split by bytes

echo mb_strcut('这个真的很nice',0,3,'utf-8'); //输出这
Copy after login

$str[0]

Treat strings as characters Collection, Chinese is not applicable!

$str = 'hello';
echo $str[0];//输出h
Copy after login

Sometimes the string contains Chinese and English numbers, etc., so customize a function that does not distinguish between Chinese and English to split the string

/**
* 
* 中英混合的字符串截取
* @param 待截取字符串 $sourcestr
* @param 截取长度 $cutlength
*/
function sub_str($sourcestr, $cutlength) {
	$returnstr = '';//待返回字符串
	$i = 0;
	$n = 0;
	$str_length = strlen ( $sourcestr ); //字符串的字节数 
	while ( ($n < $cutlength) and ($i <= $str_length) ) {
		$temp_str = substr ( $sourcestr, $i, 1 );
		$ascnum = Ord ( $temp_str ); //得到字符串中第$i位字符的ascii码 
		if ($ascnum >= 224) {//如果ASCII位高与224,
			$returnstr = $returnstr . substr ( $sourcestr, $i, 3 ); //根据UTF-8编码规范,将3个连续的字符计为单个字符  
			$i = $i + 3; //实际Byte计为3
			$n ++; //字串长度计1
		} elseif ($ascnum >= 192){ //如果ASCII位高与192,
			$returnstr = $returnstr . substr ( $sourcestr, $i, 2 ); //根据UTF-8编码规范,将2个连续的字符计为单个字符 
			$i = $i + 2; //实际Byte计为2
			$n ++; //字串长度计1
		} elseif ($ascnum >= 65 && $ascnum <= 90) {//如果是大写字母,
			$returnstr = $returnstr . substr ( $sourcestr, $i, 1 );
			$i = $i + 1; //实际的Byte数仍计1个
			$n ++; //但考虑整体美观,大写字母计成一个高位字符
		}elseif ($ascnum >= 97 && $ascnum <= 122) {
			$returnstr = $returnstr . substr ( $sourcestr, $i, 1 );
			$i = $i + 1; //实际的Byte数仍计1个
			$n ++; //但考虑整体美观,大写字母计成一个高位字符
		} else {//其他情况下,半角标点符号,
			$returnstr = $returnstr . substr ( $sourcestr, $i, 1 );
			$i = $i + 1; 
			$n = $n + 0.5; 
		}
	}
	return $returnstr;
}
Copy after login

Recommended learning: "PHP Video Tutorial

The above is the detailed content of What are the methods for intercepting php strings?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template