PHP strstr, Stristr, strpos function comparison _php tutorial

Source: Internet
Author: User
In the case of character lookup in PHP provided us with three functions strstr, STRISTR, Strpos, the use of their three is a little different. Let's take a look at the related functions.

Grammar:
String Strstr (String haystack, string needle)
Returns the string from needle start to end in haystack.
Returns False if no return value is found needle

Note: This function is case-sensitive.

Example 1

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 can match to 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. '
';
Echo $city _ch_arr_item;
$s _city= $city _ch_arr_item;
}
}
}//If you can't find a city, then see if you can find a province sometimes there is the situation: the Great Wall of Guangdong province, such a wide band of all belong to the province's provincial capitals
ElseIf (strstr ($area _ga, "Hebei")!==false) {
$s _ City= "Shijiazhuang";
}

City.dat are some of the city formats are like this
guangzhou | shenzhen | shantou | huizhou

For more detailed information, please see: http://www.bKjia.c0m/phper/18/8304359e6918876b45d02c200bc8f193.htm

The stristr () function finds the position of the first occurrence of a string in another string.

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

The code is as follows Copy Code

Echo stristr ("Hello world!", "World");
?>
Output:

world!

Strpos Introduction

The return is a character after the first two lookups are successful, and the Strpos is the location after the search succeeds. Because the location is likely to be 0, it is more appropriate to judge the lookup failure using ===false.

Strpos performance is better, if only to determine whether needle in the string haystack, the use of Strpos better, it will consume less memory and get faster execution speed. However, strpos support for special characters is not good, such as Chinese can not be very well supported


Example

The code is as follows Copy Code

Echo Strpos ("Hello world!", "Wo");
?>

Output:

6

Note: The function is case-sensitive. Use the Stripos () function if you want to search for case insensitive.


In combination with the above examples, we come to the conclusion

STRSTR case-sensitive, starting from character search if it returns true otherwise it returns false
Stristr characters are not case-sensitive, starting from character search if there is true otherwise returns false
Strpos a case-sensitive strpos the location is returned after a successful lookup. Because the location is likely to be 0, it is more appropriate to judge the lookup failure using ===false.

http://www.bkjia.com/PHPjc/629137.html www.bkjia.com true http://www.bkjia.com/PHPjc/629137.html techarticle In the case of character lookup in PHP provided us with three functions strstr, STRISTR, Strpos, the use of their three is a little different. Let's take a look at the related functions. Syntax: String St ...

  • Related Article

    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.