Home  >  Article  >  Backend Development  >  php strstr search function to find whether a string contains certain characters_PHP tutorial

php strstr search function to find whether a string contains certain characters_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:36:52990browse

PHP determines whether a string contains other characters

The following functions can be used to determine whether a string contains another string. It is very common in PHP to determine whether a string contains other characters. operation. Although it is very simple, I still wrote a few functions. The quality may not be very high, but it should be used as an exercise. If these functions can help you, I will be very happy. Among these functions, I prefer the fourth one. . .

Copy code The code is as follows:

/**
* The following functions can be used to determine whether a string contains another string.
* Determining whether a string contains other characters is a very common operation in PHP.
* Although it is very simple, I still wrote a few functions. The quality may not be very high, but it should be used as an exercise.
* If these functions can help you, I will be very happy.
*/
/**
* Use the strpos() function
* @param unknown_type $haystack
* @param unknown_type $needle
*/
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. 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));

I think the simplest one is this strpos($a, $b) !== false true if $b exists in $a, false otherwise.
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 at $ 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. You can just judge directly .

Definition and Usage
strstr() function searches for the first occurrence of a 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.

Syntax
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, then 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

Copy code The code is as follows:

echo strstr( "Hello world!","world");
?>

//Output: world!

Example 2
In this example, we will Search for the character represented by the ASCII value of "o":
Copy the code The code is as follows:

echo strstr("Hello world!",111);
?>

//Output: o world!

Example 3
Copy code The code is as follows:

$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 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 the city can be matched
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;
$s_city=$city_ch_arr_item;
}
}
}
//If you can’t find the city, see if you can find the province. Sometimes there will be such a situation: Guangdong Great Wall Broadband is like this 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";
}

The above code:
The format of some cities in city.dat is like this
Copy the code The code is as follows:

Guangzhou|Shenzhen|Shantou|Huizhou|Zhuhai|Jieyang|Foshan|Heyuan|Yangjiang|Maoming|Zhanjiang|Meizhou|Zhaoqing|Shaoguan|Chaozhou|Dongguan|Zhongshan|Qingyuan|Jiangmen|Shantou|Yunfu|Zengcheng|Conghua| Lechang|Nanxiong|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|Tumushuke|Wujiaqu|Hami|Turpan|Aksu|Kashgar|Hotian|Yining|Tacheng|Altay|Kuitun|Bole| Changji|Fukang|Korla|Artush|Usu|Lhasa|Shigatse|Yinchuan|Shizuishan|Wuzhong|Guyuan|Zhongwei|Hohhot|Baotou|Wuhai|Chifeng|Tongliao|Ordos|Hulunbuir|Bayannur|Ulanqa Bu|Khoringol|Manzhouli|Yakeshi|Zhalantun|Genhe|Erguna|Fengzhen|Xilinhot|Erenhot|Ulanhot|

Reference
Copy code The code is as follows:

echo strstr('aaaaaaaaaaboaaaaaaaaaaaaaaboxccccccccccbcccccccccccccc','box')."
n";
//Output boxccccccccccbcccccccccccccc
// Complete matching of the middle box does not stop due to the preceding b
echo strstr('aaaaaaAbaaa aaaa aaaaaaaaaaboxccccccccccccboxcccccccccccc','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
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322025.htmlTechArticlePHP Determine whether a string contains other characters The following functions can be used to determine whether a string contains another character String In PHP, it is very easy to determine whether a string contains other characters...
Statement:
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
Previous article:PhpNext article:Php