Home  >  Article  >  Backend Development  >  How to intercept string length in php

How to intercept string length in php

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-08-30 10:37:366592browse

How to intercept string length in php

PHP String Get

is used to get the specified string from the string.

Relevant functions are as follows:

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

strstr(): Finds the first occurrence of a string in another string and returns all characters from that position to 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.

Related recommendations: "PHP Tutorial"

substr()

substr() function is used to extract from a string Get part of it and return a string.

Syntax:

string substr ( string string, int start [, int length] )

Parameter description is as follows:

How to intercept string length in php

Example:

<?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
?>

Tips

If start is negative and length is less than or equal to start, 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, Returns FALSE if not found.

Syntax:

string strstr ( string string, string needle )

Parameter description is as follows:

How to intercept string length in php

Example:

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

Tips

This function is case sensitive. For case-insensitive search, use stristr() .

strchr()

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, if If not found, return FALSE.

Syntax:

string strrchr ( string string, string needle )

This function behaves the same as the strstr() function. For the meaning of the parameters, please refer to the strstr() function parameter description above.

Example:

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

Run the example, output:

|CCC

Combined with the substr() function, you can intercept all the content after the last character that appears:

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

The above is the detailed content of How to intercept string length in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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