-
- /**
- * The following functions can be used to determine whether a string contains another string.
- * It is a very common operation in PHP to determine whether a string contains other characters.
- * If these functions happen to help you, I will be very happy.
- */
- /**
- * Use the strpos() function
- * @param unknown_type $haystack
- * @param unknown_type $needle
- * @link jbxue.com
- */
- function isInString1($haystack, $needle) {
- //Prevent $needle from being at the beginning
- $ haystack = '-_-!' . $haystack;
- return (bool)strpos($haystack, $needle);
- }
- /**
- * Use string splitting
- * @param unknown_type $haystack
- * @param unknown_type $needle
- */
- function isInString2($haystack, $needle) {
- $ array = explode($needle, $haystack);
- return count($array) > 1;
- }
- /**
- * I used regular expressions, but this method is not recommended, especially if $needle contains
- * special characters, such as ^, $,/, etc.
- * @param unknown_type $haystack
- * @param unknown_type $needle
- */
- function isInString3($haystack, $needle) {
- $pattern = '/ ' . $needle . '/';
- return (bool)preg_match($pattern, $haystack);
- }
- /**
- * Use the strpos() function
- * @param unknown_type $haystack
- * @param unknown_type $needle
- */
- function isInString4($haystack, $needle) {
- return false != = strpos($haystack, $needle);
- }
- //Test
- $haystack = 'I am ITBDW';
- $needle = 'IT';
- var_dump(isInString1($haystack, $needle));
Copy the code
I think the simplest one is this strpos($a, $b) !== false If $b exists in $a, it is true, otherwise it is false.
The reason for using !== false (or === false) is that if $b is exactly at the beginning of $a, then the function will return int(0), then 0 is false, but $b is indeed located in $a, So use !== to determine the type, and make sure it is strictly false. I went to Zhongguancun Book Building last night and saw a book that used strpos === true to judge. This is extremely incorrect. . .
The book with the error is page 107 of "PHP Job Search Guide" (updated on 2012-02-26)
There are other functions natively supported by PHP, such as strstr(), stristr(), etc., which can be judged directly.
Definition and usage
The strstr() function searches for the first occurrence of one string within another string.
This function returns the rest of the string (from the matching point). Returns false if the searched string is not found.
Grammar
strstr(string,search)
Parameter Description
string required. Specifies the string to be searched for.
search required. Specifies the string to be searched for. If the argument is a number, searches for characters matching the numeric ASCII value.
Tips and Notes
Note: This function is binary safe.
Note: This function is case sensitive. For case-insensitive searches, use stristr().
Example 1:
-
- echo strstr("Hello world!","world");
- ?>
-
Copy code
//Output: world!
Example 2, in this example, we will search for the character represented by the ASCII value of "o":
-
- echo strstr("Hello world!",111);
- ?>
Copy code
//Output: o world!
Example 3:
-
-
$email = 'admin@jbxue.com'; - $domain = strstr($email, '@');
- echo $domain; // prints @ jbxue.com
$user = strstr($email, '@', true); // As of PHP 5.3.0
- echo $user; // prints admin
- ?>
- < ;/p>
-
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. '& LT; BR & GT;' Cecho $ City_ch_arr_ITEM;
- $ s_city = $ City_ch_arr_item ;
- }
- }
- }
- //If you can’t find the city, see if you can find the province. Sometimes there will be a situation like this: Guangdong Province Great Wall Broadband will all belong to the provincial capital
- elseif(strstr($area_ga ,"Hebei")!==false){
- $s_city="Shijiazhuang";
- }elseif(strstr($area_ga,"Fujian")!==false){
- $s_city="Fuzhou";
- }elseif( strstr($area_ga,"Taiwan")!==false){
- $s_city="Taipei";
- }elseif(strstr($area_ga,"Hong Kong")!==false){
- $s_city="Hong Kong";
- }elseif(strstr($area_ga,"Guangxi")!==false){
- $s_city="Nanning";
- }elseif(strstr($area_ga,"Zhejiang")!==false){
- $s_city= "Hangzhou";
- }elseif(strstr($area_ga,"Jiangsu")!==false){
- $s_city="Nanjing";
- }elseif(strstr($area_ga,"Shandong")!==false){
- $s_city="Jinan";
- }elseif(strstr($area_ga,"Anhui")!==false){
- $s_city="Hefei";
- }elseif(strstr($area_ga,"Hunan")!= =false){
- $s_city="Changsha";
- }elseif(strstr($area_ga,"Sichuan")!==false){
- $s_city="Chengdu";
- }elseif(strstr($area_ga,"Yunnan" ")!==false){
- $s_city="Kunming";
- }elseif(strstr($area_ga,"Guangdong")!==false){
- $s_city="Guangzhou";
- }elseif(strstr($ area_ga,"Guizhou")!==false){
- $s_city="Guiyang";
- }elseif(strstr($area_ga,"Tibet")!==false){
- $s_city="Lhasa";
- }elseif (strstr($area_ga,"Xinjiang")!==false){
- $s_city="Urumqi";
- }elseif(strstr($area_ga,"Mongolia")!==false){
- $s_city="Hohhot" ;
- }elseif(strstr($area_ga,"Heilongjiang")!==false){
- $s_city="Harbin";
- }elseif(strstr($area_ga,"Liaoning")!==false){
- $s_city ="Shenyang";
- }elseif(strstr($area_ga,"Jilin")!==false){
- $s_city="Changchun";
- }elseif(strstr($area_ga,"Henan")!==false) {
- $s_city="Zhengzhou";
- }elseif(strstr($area_ga,"Hubei")!==false){
- $s_city="Wuhan";
- }elseif(strstr($area_ga,"Shanxi")! ==false){
- $s_city="Taiyuan";
- }elseif(strstr($area_ga,"Shaanxi")!==false){
- $s_city="Xi'an";
- }elseif(strstr($area_ga," Gansu")!==false){
- $s_city="Lanzhou";
- }elseif(strstr($area_ga,"Ningxia")!==false){
- $s_city="Yinchuan";
- }elseif(strstr( $area_ga,"Hainan")!==false){
- $s_city="Haikou";
- }elseif(strstr($area_ga,"Jiangxi")!==false){
- $s_city="Nanchang";
- } elseif(strstr($area_ga,"Macau")!==false){
- $s_city="Macau";
- }
- //If it does not exist, Guangzhou will be displayed by default, such as the local machine
- else{
- $s_city="Guangzhou ";
- }
-
-
Copy code The code above:
Among them, city.dat contains some cities, with a format similar to this:
Guangzhou | Shenzhen | Shantou | Huizhou | Zhuhai | Jieyang | Foshan | Heyuan | Yangjiang | Maoming | Zhanjiang | Meizhou | Zhaoqing | Shaoguan | Chaozhou | Dongguan | Zhongshan | Qingyuan | Jiangmen | Shanwei | Yunfu | Zengcheng | Conghua | Lechang | Nan Xiong|Taishan|Kaiping|Heshan|Enping|Lianjiang|Leizhou|Wuchuan|Gaozhou|Huazhou|Gaoyao|Sihui|Xingning|Lufeng|Yangchun|Yingde|Lianzhou|Puning|Luoding|Beijing|Tianjin | Shanghai | Chongqing | Urumqi | Karamay | Shihezi | Alar | Tumushuk | Wujiaqu | Hami | Turpan | Aksu | Kashgar | Hotan | Yining | Tacheng | Altay | Kuitun | Bole | Changji | Fukang | Korla | Artush | Wusu | Lhasa | Shigatse | Yinchuan | Shizuishan | Wuzhong | Guyuan | Zhongwei | Hohhot | Baotou | Wuhai | Chifeng | Tongliao | Ordos | Hulunbuir | Bayannur | Ulanqab | Holin Gol|Manzhouli|Yakeshi|Zhalantun|Genhe|Ergun|Fengzhen|Xilinhot|Erenhot|Ulanhot|
Reference
-
- echo strstr('aaaaaaaaaaaboaaaaaaaaaaaboxccccccccccbcccccccccccccc','box')."
n";
- //Output boxcccccccccbcccccccccccccc
- //Completely match the box in the middle without changing the b And stop
- echo strstr('aaaaaaAbaaa aaaa aaaaaaaaaaboxccccccccccccboxcccccccccc','box')."
n";
- //Output boxccccccccccccboxcccccccccccc
- //When there are two keywords, the first stop is encountered.
- echo strstr ('Subscrtibe our to free newsletter about New Freew to','to')."
n";
- //Output to free newsletter about New Freew to
- ?>
Copy code
|