PHP determines whether the string contains other characters
The following functions can be used to determine whether a string contains another string PHP is a common operation to determine whether a string contains other characters. Although very simple, but still wrote a few functions, the quality may not be very high, the right to exercise. I'd be happy if these functions were to help you. Of these functions, I prefer the fourth one ...
Copy Code code as follows:
<?php
/**
* The following functions can be used to determine whether a string contains another string
* It is a common practice in PHP to determine whether a string contains other characters.
* Although very simple, but still wrote a few functions, the quality may not be very high, the right to exercise.
* I'd be happy if these functions are just going to help you.
*/
/**
* Use the Strpos () function
* @param unknown_type $haystack
* @param unknown_type $needle
*/
function IsInString1 ($haystack, $needle) {
Prevent $needle from starting position
$haystack = '-_-! '. $haystack;
return (BOOL) Strpos ($haystack, $needle);
}
/**
* Using string segmentation
* @param unknown_type $haystack
* @param unknown_type $needle
*/
function IsInString2 ($haystack, $needle) {
$array = Explode ($needle, $haystack);
return count ($array) > 1;
}
/**
* The use of a regular, this method is very not recommended, especially $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));
I think that's the easiest thing to do. Strpos ($a, $b)!== false True if there is a $b in $a, false otherwise.
The reason for!== false (or = = False) is if the $b is exactly at the beginning of the $a, then the function returns INT (0), then 0 is false, but $b is indeed in $a, so use!== to determine the type, and make sure it is strictly false. Last night to the Zhongguancun book building, see a book used is Strpos = = True to judge, this is extremely incorrect ...
The wrong book is "PHP Job search" 107 pages (2012-02-26 update)
The other is the PHP native support functions, such as STRSTR (), Stristr (), and so on, direct judgment on it.
Definitions and usage
The Strstr () function searches for a string that appears for the first time in another string.
The function returns the remainder of the string (from a match point). returns false if the searched string is not found.
Grammar
Strstr (String,search)
parameter Description
String required. Specify the string to be searched for.
Search required. Specify the string to search for. If the argument is a number, searches for characters that match the numeric ASCII value.
Tips and Comments
Note: This function is binary safe.
Note: This function is sensitive to case sensitivity. For case insensitive searches, use STRISTR ().
Example 1
Copy Code code as follows:
<?php
Echo strstr ("Hello world!", "World");
?>
Output: world!
Example 2
In this case, we'll search for the characters that the ASCII value of "O" represents:
Copy Code code as follows:
<?php
Echo strstr ("Hello world!", 111);
?>
Output: o world!
Example 3
Copy Code code as follows:
<?php
$email = ' admin@jb51.net ';
$domain = Strstr ($email, ' @ ');
Echo $domain; Prints @jb51. Net
$user = Strstr ($email, ' @ ', true); As of PHP 5.3.0
Echo $user; Prints admin
?>
Copy Code code 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 you can match the location 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. ' <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";
}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= "Macao";
}
If none exists, the default display is Guangzhou, such as Local machine.
else{
$s _city= "Guangzhou";
}
As above code:
Where City.dat are some of the city formats are like this
Copy Code code as follows:
guangzhou | shenzhen | shantou | huizhou | zhuhai | jieyang | foshan | heyuan | Yangjiang | maoming | zhanjiang | Meizhou | zhaoqing | shaoguan | chaozhou | dongguan | zhongshan | qingyuan | jiangmen | Shanwei | Yunfu | Zengcheng | conghua | Lechang | Nanxiong | taishan | Kaiping | heshan | enping | Lianjiang | Leizhou | Wuchuan | Gaozhou | Huazhou | gaoyao | sihui | xingning | Lufeng | yangchun | china | germany | |--puning City | beijing | tianjin | shanghai | chongqing | urumqi | karamay | shihezi | alar | Tumshuk | five Hami | turpan | Yining | Tacheng | altay | Kuitun | Bole | changji | Fukang | korla | Atushi | ursula | lhasa Shigatse | yinchuan | shizuishan | wuzhong | guyuan | zhongwei | hohhot | baotou | Wuhai | chifeng | tongliao | ordos | hulunbeier | Bayan nur | wulanchabu | Huolinguolei | Yakeshi | Zalantun | Genhe |
Reference
Copy Code code as follows:
<?php
echo strstr (' AAAAAAAAAAABOAAAAAAAAAAAABOXCCCCCCCCCBCCCCCCCCCCCCCC ', ' box '). " <br>\n ";
Output BOXCCCCCCCCCBCCCCCCCCCCCCCC
The box in the middle is not stopped by the front and the B.
echo strstr (' aaaaaaabaaa aaaa aaaaaaaaaboxccccccccccccboxccccccccccc ', ' box '). " <br>\n ";
Output BOXCCCCCCCCCCCCBOXCCCCCCCCCCC
When there are two keywords, the first stop is encountered.
echo strstr (' Subscrtibe We to free newsletter about the New freew to ' and ' to '). " <br>\n ";
Output to free newsletter about the New freew to
?>