Home > Backend Development > PHP Problem > How to intercept string length in php

How to intercept string length in php

爱喝马黛茶的安东尼
Release: 2023-02-23 13:50:01
Original
6701 people have browsed it

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] )
Copy after login

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
?>
Copy after login

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 )
Copy after login

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
?>
Copy after login

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 )
Copy after login

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, "|");
?>
Copy after login

Run the example, output:

|CCC
Copy after login

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);
?>
Copy after login

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!

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