Comparison of php strstr, stristr, strpos functions_PHP tutorial

WBOY
Release: 2016-07-13 17:13:23
Original
1844 people have browsed it

When searching for characters, PHP provides us with three functions strstr, stristr, and strpos. Their usage is slightly different. Let’s introduce the related functions.

Syntax:
string strstr (string haystack, string needle)
Returns the string from needle to end in haystack.
If there is no return value, that is, no needle is found, return FALSE

Note: This function is case-sensitive.

Example 1

The code is as follows
 代码如下 复制代码

$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(strstr($area_ga,"市")){
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;
  }
}
}//如果找不到市 那么看看是不是能找到省 有时会有这样的情况:广东省长城宽带 这样的一律归属到该省省府
elseif(strstr($area_ga,"河北")!==false){
$s_city="石家庄";
}

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 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;
 代码如下 复制代码

echo stristr("Hello world!","WORLD");
?>
输出:

world!

$s_city=$city_ch_arr_item;

}

}

}//If you can’t find the city, then see if you can find the province. Sometimes there will be a situation like this: Guangdong Great Wall Broadband will all belong to the provincial government elseif(strstr($area_ga,"Hebei")!==false){ $s_city="Shijiazhuang";

}

city.dat contains some cities. The format is like this
Guangzhou|Shenzhen|Shantou|Huizhou

 代码如下 复制代码

echo strpos("Hello world!","wo");
?>

输出:

6

For more details, please check: http://www.bKjia.c0m/phper/18/8304359e6918876b45d02c200bc8f193.htm

stristr()
function finds the first occurrence of a string in another string. If successful, returns the rest of the string (from the point of the match). If the string is not found, returns false.

?>
The code is as follows

Copy code

echo stristr("Hello world!","WORLD");
Output: world! strpos introduction Compared with the first two, characters are returned after a successful search, while strpos returns a position after a successful search. Because the position may be 0, it is more appropriate to use ===false to judge the search failure. The performance of strpos is better. If you just determine whether the needle is in the string haystack, it is better to use strpos. It will occupy less memory and achieve faster execution speed. However, strpos does not support special characters well, such as Chinese Example
The code is as follows Copy code
echo strpos("Hello world!","wo");<🎜> ?> Output: 6 Note: This function is case sensitive. For case-insensitive searches, use the stripos() function. Combining the above examples we draw the conclusion strstr is case-sensitive, starting from the character, if there is one, it returns true, otherwise it returns false stristr characters are not case-sensitive. If you start searching from the character, it will return true. Otherwise, it will return false strpos is case-sensitive. After strpos search is successful, the position will be returned. Because the position may be 0, it is more appropriate to use ===false to judge the search failure.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629137.htmlTechArticle provides us with three functions strstr, stristr, strpos in php during character search. The usage is a bit different. Let’s introduce the related functions. Syntax: string st...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template