如何使用strpos确定输入字符串中是否存在字符串? [英] How to use strpos to determine if a string exists in input string?

查看:78
本文介绍了如何使用strpos确定输入字符串中是否存在字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$filename = 'my_upgrade(1).zip';
$match = 'my_upgrade';

if(!strpos($filename, $match))
    {
    die();
    }
else 
   {
   //proceed
   }

在上面的代码中,当文件名不包含文本字符串"my_upgrade"时,我试图退出脚本.但是,在给出的示例中,它不会死,因为" my_upgrade(1).zip "包含字符串" my_upgrade ".

In the code above, I'm trying to die out of the script when the filename does not contain the text string "my_upgrade". However, in the example given, it should not die since "my_upgrade(1).zip" contains the string "my_upgrade".

我想念什么?

推荐答案

strpos如果找不到该字符串,则返回false,如果在开头找到该字符串,则返回0.使用身份运算符来区分两者:

strpos returns false if the string is not found, and 0 if it is found at the beginning. Use the identity operator to distinguish the two:

if (strpos($filename, $match) === false) {

顺便说一句,官方文件.

这篇关于如何使用strpos确定输入字符串中是否存在字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆