如何使用strpos确定输入字符串中是否存在字符串?

编程入门 行业动态 更新时间:2024-10-27 22:25:58
本文介绍了如何使用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确定输入字符串中是否存在字符串?

本文发布于:2023-10-29 20:39:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1540689.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   如何使用   是否存在   strpos

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!