Comparison of strstr, stristr, and strpos functions in php

Source: Internet
Author: User
Tags relative

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.

Instance 1

The code is as follows: Copy code

$ City_str = fopen (cgi_path. "/data/weather/city. dat", "r ");
$ City_ch = fread ($ city_str, filesize (cgi_path. "/data/weather/city. dat "));
$ City_ch_arr = explode ("|", $ city_ch );
// If it matches the city
If (strstr ($ area_ga, "city ")){
Foreach ($ city_ch_arr as $ city_ch_arr_item ){
If (@ strstr ($ area_ga, $ city_ch_arr_item )){
Echo $ area_ga. '<br> ';
Echo $ city_ch_arr_item;
$ S_city = $ city_ch_arr_item;
  }
}
} // 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.
Elseif (strstr ($ area_ga, "Hebei ")! = False ){
$ S_city = "Shijiazhuang ";
}

In city. dat, some city formats are as follows:
Guangzhou | Shenzhen | Shantou | Huizhou

For more details, see: http://www.111cn.net/phper/18/8304359e6918876b45d02c200bc8f193.htm

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

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

The code is as follows: Copy code

<? Php
Echo stristr ("Hello world! "," WORLD ");
?>
Output:

World!

 

Strpos introduction

Returns a character relative to the first two, and a position relative to the first two. Because the location may be 0, it is more appropriate to use = false to determine if the search 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.


Example

The code is as follows: Copy code

<? Php
Echo strpos ("Hello world! "," Wo ");
?>

Output:

6

Note: This function is case sensitive. Use the stripos () function to perform case-insensitive searches.


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, it returns a location. Because the location may be 0, it is more appropriate to use = false to determine if 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.