PHP STRSTR looks for a lookup function that contains certain characters in a string

  • 2020-03-31 20:48:31
  • OfStack

Strstr  Definition and usage

Note: this function is binary safe.

grammar

STRSTR (string, search, before_search)

parameter describe
string A necessity. Specifies the string to be searched.
search

A necessity. Specifies the string to search for.

If the parameter is a number, the search matches the corresponding number ASCII The character of the value.

before_search

Optional. The default value is "false" The Boolean value of.

If set to "true" , it will return  search  The part of the string before the parameter first appears.

The technical details

The return value: Returns the remainder of the string (from the match point). Returns if the searched string is not found FALSE .
PHP Version: 4+
Update log: in PHP 5.3 In, added  before_search  Parameters.

More instances

Example 1

Searches the string with the ASCII value of "o" and returns the rest of the string:


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

return

O world!

Example 2
Returns the part of the string before "world" first appeared:


<?php

echo strstr("Hello world!","world",true);

Results:

Hello!

Example 3

Look for "Shanghai" in "I love Shanghai!" Returns the rest of the string:


<?php
echo strstr("I love Shanghai!","Shanghai");
?>

  Results:

Shanghai!

PHP determines whether a string contains other characters

The following functions are used to determine whether a string contains another string. It is common 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, just exercise. I'll be happy if these functions work for you. Of these, I prefer the fourth...


<?php 
 
 
function isInString1($haystack, $needle) { 
//Prevent $needle from being in the starting position
$haystack = '-_-!' . $haystack; 
return (bool)strpos($haystack, $needle); 
} 
 
function isInString2($haystack, $needle) { 
$array = explode($needle, $haystack); 
return count($array) > 1; 
} 
/** 
*  Using regularization, this method is not recommended, especially  $needle  Contained in the  
*  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); 
} 
 
function isInString4($haystack, $needle) { 
return false !== strpos($haystack, $needle); 
} 
//test
$haystack = 'I am ITBDW'; 
$needle = 'IT'; 
var_dump(isInString1($haystack, $needle)); 

I think the easiest one is this strpos($a, $b)! == false if there is $b in $a, true, otherwise false.
Use! == false (or === false) because if $b is exactly at the beginning of $a, then this function returns int(0), so 0 is false, but $b is in $a, so use! == to determine the type, make sure it is strictly false. Last night I went to zhongguancun book building and saw a book using strpos = = = true.

The error occurred on page 107 of the PHP job search guide (updated February 26, 2012)

Definition and usage

The STRSTR () function searches for the first occurrence of a string in another string.
This function returns the rest of the string (from the match point). If the searched string is not found, false is returned.

grammar
STRSTR (string, search)

Parameters to describe
The string must be. Specifies the string to be searched.
Search a necessity. Specifies the string to search for. If the parameter is a number, the character matching the numeric ASCII value is searched.

Hints and comments
Note: this function is binary safe.

Comment: this function is case sensitive. For case-insensitive searches, use (link: #).

Example 1


<?php 
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" :


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

// output: o world!

Example 3


<?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 
?> 

$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 to the city
if(strstr($area_ga," The 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 a city then see if you can find a province sometimes there is such a situation: guangdong province Great Wall broadband such as 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," Ji Lin ")!==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 none of them exist, the default display is guangzhou such as local machine
else{ 
$s_city=" Guangzhou "; 
}

The above code:
The format of city. Dat is like this

Shantou guangzhou shenzhen | | | | | zhuhai jieyang huizhou | | foshan heyuan yangjiang | | maoming zhanjiang, | | | | zhaoqing of meizhou shaoguan zhongshan | | | | dongguan chaozhou qingyuan | | jiangmen shanwei yunfu | | zengcheng | | conghua lechang male | | south taishan kaiping | | heshan | enping | lianjiang | leizhou | gaozhou county of wuchuan | | HuaZhou high | | sihui | xin | lufeng | yangchun | German | | even states puning | luo certain | Beijing | | | | | Shanghai chongqing tianjin urumqi karamay shihezi | | the aral | toumchouq | wujiaqu | hami turpan | | akzo yining | | | | hotan region of tuscaloosa | altay | kuytun | bo le | changji | fukang | korla | figure attash | WuSu | | xigaze in Lhasa | | in yinchuan making | hui | evalution | | the DCS Hohhot | | in baotou in wuhai | chifeng | tgo | | ordos hulun buir | bayinnaoer wulanchabu | | hollingsworth Guo Le | manchuria dental | g | stone zhalantun root | | erguna river | powered "| | tin event, erlianhot | ulanhot |

reference


<?php 
echo strstr('aaaaaaaaaaaboaaaaaaaaaaaaboxcccccccccbcccccccccccccc','box')."<br>n"; 
//Output boxcccccccccbcccccccccccccc
//A complete match of the box in the middle does not stop with the previous b
echo strstr('aaaaaaAbaaa aaaa aaaaaaaaaboxccccccccccccboxccccccccccc','box')."<br>n"; 
//Output boxccccccccccccboxccccccccccc
//There are two keywords when the first stop is encountered.
echo strstr('Subscrtibe our to free newsletter about New Freew to','to')."<br>n"; 
//Output to free newsletter about New Freew to
?> 

Comment: this function is case sensitive. To perform a case-insensitive search, use the stristr() function.


Related articles: