Introduction and use example of four functions related to strings in php: substr, strrchr, strstr, and ereg _ php instance

Source: Internet
Author: User
This article mainly introduces four functions and examples of using strings in php. For more information, see 1. Obtain some strings.

The Code is as follows:

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

This function extracts the start character of the string from the start character. If start is a negative number,

The value is counted from the end of the string. If the parameter length can be omitted, but it is a negative number, it indicates that the maximum length is obtained.

The Code is as follows:


Echo substr ("abcdef", 1, 3); // return "bcd"
Echo substr ("abcdef",-2); // return "ef"
Echo substr ("abcdef",-3, 1); // return "d"
Echo substr ("abcdef", 1,-1); // return "bcde"

2. Obtain the string starting from the last occurrence of a character.

The Code is as follows:

String strrchr (string haystack, string needle );

This function is used to find the final position of the character needle in the string haystack and

Returns a string between the ends of the haystack operation. If no needle is found, false is returned.

The Code is as follows:


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

Output: // localhost/test. php


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

The Code is as follows:

String strstr (string haystack, string needle );

This function returns the string that needle first appears at the haystack end to the end of the haystack. If no needle is found, false is returned.

4. String comparison and parsing.

The Code is as follows:

Int ereg (string pattern, string, array [regs]);

This function uses the pattern Rule to parse and compare strings. The value returned from the comparison result is placed in the array parameter regs, the regs [0] content is the original string, regs [1] is the first regular string, regs [2] is the second regular string, and so on. If the regs parameter is omitted, only comparison is performed. If it is found, the return value is true.

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 ";
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.