Home  >  Article  >  Backend Development  >  Introduction and usage examples of the four string-related functions substr, strrchr, strstr, ereg in PHP_PHP Tutorial

Introduction and usage examples of the four string-related functions substr, strrchr, strstr, ereg in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:31:56802browse

1. Get part of the string.

Copy code The code is as follows:
string substr(string string, int start, int [length]);

This function extracts length characters from the start position of the string string. If start is a negative number,

is counted from the end of the string. If the omitted parameter length exists but is a negative number, it means that the length character from the bottom is obtained.

Copy code The code is as follows:

echo substr ( "abcdef" , 1 , 3 ); // Return "bcd"
echo substr ( "abcdef" , - 2 ); // Returns "ef"
echo substr ( "abcdef" , - 3 , 1 ); // Returns "d"
echo substr ( "abcdef " , 1 , - 1 ); // Return "bcde"

2. Get the string starting from the last occurrence of a certain character.

Copy code The code is as follows:
string strrchr(string haystack, string needle);

This function is used to find the last occurrence position of the character needle in the string haystack, and start from this position to the string

The string returned between the end of haystack. If needle is not found, false is returned.

Copy code The code is as follows:

$PATH="http://localhost/test/test.php";
$dir = substr( strrchr( $PATH, ":" ), 1 );
echo $dir;

output://localhost/test/test.php


3. Return the string from the beginning to the end of a certain string in the string.

Copy code The code is as follows:
string strstr(string haystack, string needle);

This function returns the string from when needle first appears in haystack to the end of haystack. Returns false if needle is not found.

4. String comparison analysis.

Copy code The code is as follows:
int ereg(string pattern, string string, array [regs]);

This function uses pattern rules to parse and compare string strings. The value returned by the comparison result is placed in the array parameter regs. The content of regs[0] is the original string, regs[1] is the first string that conforms to the rules, and regs[2] is the second string that conforms to the rules. string, and so on. If the parameter regs is omitted, it will simply be compared, and the return value will be true if found.

Copy code The code is as follows:

if ( eregi ( "^ [ _/.0-9a-z- ] + @( [ 0-9a-z ][ 0-9a-z- ] +/.)+ [ a-z ]{ 2,3 }$ " , $email )) {
echo "Your email has passed the preliminary Check" ;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/760293.htmlTechArticle1. Get part of the string. Copy the code as follows: string substr(string string, int start, int [length]); This function takes out the string starting from the start position of string string len...
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