萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> php strstr 函數判斷字符串否存在

php strstr 函數判斷字符串否存在

$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.'<br>';
   echo $city_ch_arr_item;
   $s_city=$city_ch_arr_item;
  }
}
}//如果找不到市 那麼看看是不是能找到省 有時會有這樣的情況:廣東省長城寬帶 這樣的一律歸屬到該省省府
elseif(strstr($area_ga,"河北")!==false){
$s_city="石家莊";
}elseif(strstr($area_ga,"福建")!==false){
$s_city="福州";
}elseif(strstr($area_ga,"台灣")!==false){
$s_city="台北";
}elseif(strstr($area_ga,"香港")!==false){
$s_city="香港";
}elseif(strstr($area_ga,"廣西")!==false){
$s_city="南寧";
}elseif(strstr($area_ga,"浙江")!==false){
$s_city="杭州";
}elseif(strstr($area_ga,"江蘇")!==false){
$s_city="南京";
}elseif(strstr($area_ga,"山東")!==false){
$s_city="濟南";
}elseif(strstr($area_ga,"安徽")!==false){
$s_city="合肥";
}elseif(strstr($area_ga,"湖南")!==false){
$s_city="長沙";
}elseif(strstr($area_ga,"四川")!==false){
$s_city="成都";
}elseif(strstr($area_ga,"雲南")!==false){
$s_city="昆明";
}elseif(strstr($area_ga,"廣東")!==false){
$s_city="廣州";
}elseif(strstr($area_ga,"貴州")!==false){
$s_city="貴陽";
}elseif(strstr($area_ga,"西藏")!==false){
$s_city="拉薩";
}elseif(strstr($area_ga,"新疆")!==false){
$s_city="烏魯木齊";
}elseif(strstr($area_ga,"蒙古")!==false){
$s_city="呼和浩特";
}elseif(strstr($area_ga,"黑龍江")!==false){
$s_city="哈爾濱";
}elseif(strstr($area_ga,"遼寧")!==false){
$s_city="沈陽";
}elseif(strstr($area_ga,"吉林")!==false){
$s_city="長春";
}elseif(strstr($area_ga,"河南")!==false){
$s_city="鄭州";
}elseif(strstr($area_ga,"湖北")!==false){
$s_city="武漢";
}elseif(strstr($area_ga,"山西")!==false){
$s_city="太原";
}elseif(strstr($area_ga,"陝西")!==false){
$s_city="西安";
}elseif(strstr($area_ga,"甘肅")!==false){
$s_city="蘭州";
}elseif(strstr($area_ga,"寧夏")!==false){
$s_city="銀川";
}elseif(strstr($area_ga,"海南")!==false){
$s_city="海口";
}elseif(strstr($area_ga,"江西")!==false){
$s_city="南昌";
}elseif(strstr($area_ga,"澳門")!==false){
$s_city="澳門";
}
//如果都不存在 就是默認顯示廣州 比如本地機
else{
$s_city="廣州";
}

如上代碼:
其中 city.dat中是一些城市 格式是這樣的
廣州|深圳|汕頭|惠州|珠海|揭陽|佛山|河源|陽江|茂名|湛江|梅州|肇慶|韶關|潮州|東莞|中山|清遠|江門|汕尾|雲浮|增城|從化|樂昌|南雄|台山|開平|鶴山|恩平|廉江|雷州|吳川|高州|化州|高要|四會|興寧|陸豐|陽春|英德|連州|普寧|羅定|北京|天津|上海|重慶|烏魯木齊|克拉瑪依|石河子|阿拉爾|圖木舒克|五家渠|哈密|吐魯番|阿克蘇|喀什|和田|伊寧|塔城|阿勒泰|奎屯|博樂|昌吉|阜康|庫爾勒|阿圖什|烏蘇|拉薩|日喀則|銀川|石嘴山|吳忠|固原|中衛|呼和浩特|包頭|烏海|赤峰|通遼|鄂爾多斯|呼倫貝爾|巴彥淖爾|烏蘭察布|霍林郭勒|滿洲裡|牙克石|扎蘭屯|根河|額爾古納|豐鎮|錫林浩特|二連浩特|烏蘭浩特|


參考

echo strstr('aaaaaaaaaaaboaaaaaaaaaaaaboxcccccccccbcccccccccccccc','box')."n"; 

05 // 完整匹配中間的box 不因前而的b而停止 

06 echo strstr('aaaaaaAbaaa aaaa aaaaaaaaaboxccccccccccccboxccccccccccc','box')."n"; 

07 // 有兩個關鍵字時, 遇到第一個停止. 

08 echo strstr('Subscrtibe our to free newsletter about New Freew to','to')."n"; 

09   
吧。

copyright © 萬盛學電腦網 all rights reserved