Comparison of phpstrstr, stristr, and strpos functions

Source: Internet
Author: User
Three functions strstr, stristr, and strpos are provided in php for character search. the usage of these three functions is somewhat different. next we will introduce the relevant functions.

Three functions strstr, stristr, and strpos are provided in php for character search. the usage of these three functions is somewhat different. next we will introduce the relevant functions.

Syntax:

String strstr (string haystack, string needle)

Returns the string starting from needle to ending in haystack.

If no value is returned, that is, no needle is found, FALSE is returned.

Note: This function is case sensitive.

The instance code is as follows:

  1. $ City_str = fopen (cgi_path. "/data/weather/city. dat", "r ");
  2. $ City_ch = fread ($ city_str, filesize (cgi_path. "/data/weather/city. dat "));
  3. $ City_ch_arr = explode ("|", $ city_ch );
  4. // If it matches the city
  5. If (strstr ($ area_ga, "city ")){
  6. Foreach ($ city_ch_arr as $ city_ch_arr_item ){
  7. If (@ strstr ($ area_ga, $ city_ch_arr_item )){
  8. Echo $ area_ga .'
    ';
  9. Echo $ city_ch_arr_item;
  10. $ S_city = $ city_ch_arr_item;
  11. }
  12. }
  13. } // If the city cannot be found, check if the province can be found. sometimes the following situation occurs: Guangdong Great Wall broadband belongs to the provincial government.
  14. Elseif (strstr ($ area_ga, "Hebei ")! = False ){
  15. $ S_city = "Shijiazhuang ";
  16. }

In city. dat, some city formats are as follows:

Guangzhou | Shenzhen | Shantou | Huizhou

For more details, see: http://www.phpfensi.com

The stristr () function is used to locate the first occurrence of a string in another string.

If the string is successful, the rest of the string (from the matching point) is returned. if the string is not found, false is returned.

The instance code is as follows:

  1. Echo stristr ("Hello world! "," WORLD ");
  2. ?>

Output:

World!

Strpos introduction

Compared with the characters returned after the first two queries are successful, the return position is returned after the strpos query is successful. because the position may be 0, it is more appropriate to use = false to judge if the query fails.

Strpos has better performance. if you only judge whether the needle is in the string haystack, strpos is better. it will occupy less memory and get faster execution speed. however, strpos does not support special characters, such as Chinese characters.

The instance code is as follows:

  1. Echo strpos ("Hello world! "," Wo ");
  2. ?>

Output:

6

Note: This function is case sensitive. to search for a case-insensitive function, use the stripos () function.

Based on the above example, we can conclude that

Strstr is case-sensitive. it is found from characters. If yes, true is returned. otherwise, false is returned.

Stristr characters are case-insensitive. start with the character. If yes, true is returned. otherwise, false is returned.

Strpos is case-sensitive. after strpos is successfully searched, the returned position is returned. because the position may be 0, it is more appropriate to use = false to determine whether the search fails.

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.