


php strstr search function to find whether a string contains certain characters
PHP Determine whether a string contains other characters
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. Among these functions, I prefer the fourth one. . .
<?php /** * 以下几个函数均可用来判断某字符串是否包含另外一个字符串 * PHP 中判断一个字符串是否包含其它字符是很常见的操作。 * 虽然很简单,但还是写了几个函数,质量可能不是很高,权当锻炼。 * 如果这几个函数恰好能帮上你的忙,我将会很高兴的。 */ /** * 利用一下 strpos() 函数 * @param unknown_type $haystack * @param unknown_type $needle */ function isInString1($haystack, $needle) { //防止$needle 位于开始的位置 $haystack = '-_-!' . $haystack; return (bool)strpos($haystack, $needle); } /** * 利用字符串分割 * @param unknown_type $haystack * @param unknown_type $needle */ function isInString2($haystack, $needle) { $array = explode($needle, $haystack); return count($array) > 1; } /** * 用了一下正则,这种方法十分不建议,尤其是 $needle 中包含 * 特殊字符,如 ^,$,/ 等等 * @param unknown_type $haystack * @param unknown_type $needle */ function isInString3($haystack, $needle) { $pattern = '/' . $needle . '/'; return (bool)preg_match($pattern, $haystack); } /** * 利用一下 strpos() 函数 * @param unknown_type $haystack * @param unknown_type $needle */ function isInString4($haystack, $needle) { return false !== strpos($haystack, $needle); } //测试 $haystack = 'I am ITBDW'; $needle = 'IT'; var_dump(isInString1($haystack, $needle));
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 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)
Others include 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 in 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, 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
<?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(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="广州"; }
The above code:
The format of some cities in city.dat is like this
广州|深圳|汕头|惠州|珠海|揭阳|佛山|河源|阳江|茂名|湛江|梅州|肇庆|韶关|潮州|东莞|中山|清远|江门|汕尾|云浮|增城|从化|乐昌|南雄|台山|开平|鹤山|恩平|廉江|雷州|吴川|高州|化州|高要|四会|兴宁|陆丰|阳春|英德|连州|普宁|罗定|北京|天津|上海|重庆|乌鲁木齐|克拉玛依|石河子|阿拉尔|图木舒克|五家渠|哈密|吐鲁番|阿克苏|喀什|和田|伊宁|塔城|阿勒泰|奎屯|博乐|昌吉|阜康|库尔勒|阿图什|乌苏|拉萨|日喀则|银川|石嘴山|吴忠|固原|中卫|呼和浩特|包头|乌海|赤峰|通辽|鄂尔多斯|呼伦贝尔|巴彦淖尔|乌兰察布|霍林郭勒|满洲里|牙克石|扎兰屯|根河|额尔古纳|丰镇|锡林浩特|二连浩特|乌兰浩特|
Reference
<?php echo strstr('aaaaaaaaaaaboaaaaaaaaaaaaboxcccccccccbcccccccccccccc','box')."<br>\n"; //输出boxcccccccccbcccccccccccccc // 完整匹配中间的box 不因前而的b而停止 echo strstr('aaaaaaAbaaa aaaa aaaaaaaaaboxccccccccccccboxccccccccccc','box')."<br>\n"; //输出boxccccccccccccboxccccccccccc // 有两个关键字时, 遇到第一个停止. echo strstr('Subscrtibe our to free newsletter about New Freew to','to')."<br>\n"; //输出to free newsletter about New Freew to ?>
For more articles related to the search function of php strstr to find whether a string contains certain characters, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
