PHP string interception tutorial

PHPz
Release: 2019-08-01 16:27:58
Original
15103 people have browsed it

获Php string Get SUBSTR and StRSTR Function

PHP string interception tutorial

Recommended tutorial: Php online tutorial


Php string to get

to obtain specified characters from the string string. Relevant functions are as follows:

 ●substr(): Get a part of it from a string

 ●strstr(): Find the position where the string first appears in another string, and return the position from that position to All characters at the end of the string

 ●subchr(): Same as strstr()

 ●strrchr(): Find the last occurrence of a string in another string and return all characters from that position to the end of the string Character

substr()

Syntax:

strrchr(): The function is used to get a part of it from a string and return a string.

string substr ( string string, int start [, int length] )
Copy after login
The parameter description is as follows:

Parameter DescriptionstringThe string to be processedstartStart position of the string, the starting position is 0, if it is negative, start from Starting from the specified position at the end of the stringlengthOptional, the length returned by the string, the default is until the end of the string, if it is negative, it is returned from the end of the stringExample:
<?php
echo substr(&#39;abcdef&#39;, 1);      //输出 bcdef
echo substr(&#39;abcdef&#39;, 1, 2);   //输出 bc
echo substr(&#39;abcdef&#39;, -3, 2);  //输出 de
echo substr(&#39;abcdef&#39;, 1, -2);  //输出 bcd
?>
Copy after login

Tips: If start is a negative number and length is less than or equal to start, then length is 0.

strstr()

Find the first occurrence of a string in another string and return all characters from that position to the end of the string, or FALSE if not found. Syntax:

string strstr ( string string, string needle )
Copy after login

The parameter description is as follows:

Parameter DescriptionstringThe string to be processed needleTo be The string to search for, if it is a number , then search for characters matching the numeric ASCII value

例子:

<?php
$email = &#39;user@5idev.com&#39;;
$domain = strstr($email, &#39;@&#39;);
echo $domain;			// 输出 @5idev.com
?>
Copy after login

提示:该函数对大小写敏感。如需进行大小写不敏感的查找,请使用 stristr() 。

strchr()

同 strstr() 。

strrchr()

查找字符串在另一个字符串中最后一次出现的位置,并返回从该位置到字符串结尾的所有字符,如果没找到则返回 FALSE。

语法:

string strrchr ( string string, string needle )
Copy after login

该函数行为同 strstr() 函数,参数意义可参见上面 strstr() 函数参数说明。

例子:

<?php
$str="AAA|BBB|CCC";
echo strrchr($str, "|");
?>
Copy after login

运行例子,输出:

|CCC
Copy after login

结合 substr() 函数便可以实现 截取某个最后出现的字符后面的所有内容 这一功能:

<?php
$str="AAA|BBB|CCC";
echo substr(strrchr($str, "|"), 1);
?>
Copy after login

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