PHP strstr, Stristr, Strpos function comparison

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags different echo find function functions it is note php

In the character lookup in PHP for us to provide three functions strstr, STRISTR, Strpos, the use of their three a little different. Let's take a look at the related functions here.

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

Note: This function is case sensitive.

Instance 1


$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 you can match 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 you can't find the city, then see if you can find a province. Sometimes there is such a situation: The Great Wall of Guangdong province, such as broadband to belong to the provincial capitals
ElseIf (Strstr ($area _ga, "Hebei")!==false) {
$s _city= "Shijiazhuang";
}

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

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

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

If successful, the remainder of the string is returned (from the match point). Returns False if the string is not found.


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

world!

Strpos Introduction

Relative to the previous two find success after the return of the character, Strpos after the success of the search is to return the position. Because the position may be 0, it is more appropriate to judge the lookup failure using ===false.

Strpos performance is good, if only to determine whether needle in the string haystack, then use strpos better, it will take up less memory and get faster execution speed. But Strpos is not good for special characters, for example, it can't support Chinese very well.


Example


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

Output:

6

Note: This function is case sensitive. For a case-insensitive search, use the Stripos () function.


Combining the above examples, we conclude that

Strstr the case, starting from the character. Returns False if there is return true
Stristr characters are not case-sensitive, look at the character and return False if you have returned true
Strpos difference Case Strpos After a successful lookup, the position is returned. Because the position may be 0, it is more appropriate to judge the lookup failure using ===false.

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.