php strpos无效,簡單的PHP strpos功能不起作用,為什么?
Why isn't this standalone code working:
為什么這個獨立代碼不起作用:
$link = 'https://google.com';
$unacceptables = array('https:','.doc','.pdf', '.jpg', '.jpeg', '.gif', '.bmp', '.png');
foreach ($unacceptables as $unacceptable)
{
if (strpos($link, $unacceptable) === true) {
echo 'Unacceptable Found
';
}
else {
echo 'Acceptable!
';
}
}
It's printing acceptable every time even though https is contained within the $link variable.
即使https包含在$ link變量中,每次打印都是可接受的。
6 个解决方案
#1
53
When in doubt, read the docs:
如有疑問,請閱讀文檔:
[strpos] Returns the numeric position of the first occurrence of needle in the haystack string.
[strpos]返回haystack字符串中第一次出現needle的數字位置。
So you want to try something more like:
所以你想嘗試更像的東西:
// ...
if (strpos($link, $unacceptable) !== false) {
Because otherwise strpos is returning a number, and you're looking for a boolean true.
因為否則strpos返回一個數字,你正在尋找一個布爾值true。
#2
7
strpos() does not return true when it finds a match, it returns the position of the first matching string. Watchout, if the match is a the beginning of the string it will return an index of zero which will compare as equal to false unless you use the === operator.
strpos()在找到匹配項時不返回true,它返回第一個匹配字符串的位置。注意,如果匹配是字符串的開頭,它將返回零索引,除非你使用===運算符,否則它將等於false。
#3
5
Your failure condition is wrong.
你的失敗情況是錯誤的。
strpos returns false if match is not found, so you need to explicitly check
如果未找到匹配,則strpos返回false,因此您需要顯式檢查
if (strpos($link, $unacceptable) !== false) {
#4
1
Strpos always return position like you search "httpsL" in your string('https://google.com';) then it return 0th position and PHP evaluate it as false.
Strpos始終返回位置,就像您在字符串中搜索“httpsL”('https://google.com';;)然后返回第0個位置,PHP將其評估為false。
please see this link:(Hope its very usefull for you): http://php.net/manual/en/function.strpos.php
請看這個鏈接:(希望它對你非常有用):http://php.net/manual/en/function.strpos.php
#5
0
if (strpos($link, $unacceptable))
{
} else {
/* What you want to do when it is false */
}
#6
0
strpos
strpos
function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE.
function可以返回布爾值FALSE,但也可以返回一個非布爾值,其值為FALSE。
So I did like this
所以我確實喜歡這個
if (strpos($link, $unacceptable) !== false) {
//Code
}
php strpos无效,簡單的PHP strpos功能不起作用,為什么?相关推荐
- 簡單編譯內核 linux kernel gnu
2019独角兽企业重金招聘Python工程师标准>>> 需要 make, gcc 下戴內核檔 http://www.kernel.org 成為 root 進入 /usr/src 解壓 ...
- JSP + AJAX 打造簡單聊天室
本文代碼下載地址:http://download.csdn.net/source/798197 一個簡單的聊天室程式,但基本包含了簡單的AJAX的使用方法,可以做為簡單的Demo用來學習. 整個程式包 ...
- D - F e n d 簡 單 教 學 DOSBOX Easy Shell 【玩DOS游戏的必备软件】
分享一下我老师大神的人工智能教程.零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow D - F e n ...
- matlab 白色像素点,MATLAB 簡單的計算白色輪廓中像素點的個數
近來,有朋友問到,如何計算白色輪廓中的像素點的個數.我在這里就舉一個超級簡單的例子,就是假設一副二值圖片,其背景是黑色的,而你的邊緣是白色的,而且你的白色邊緣中不包含黑色的點,就如附件中的那個圖像.下 ...
- Perl 第二章 簡單變量
基本上,簡單變量就是一個數據單元,這個單元可以是數字或字符串. [ 整型 ] PERL最常用的簡單變量,由於其與其它語言基本相同,不再贅述.例︰ $x = 12345; if (1217 + 116 ...
- python3librequest_python3.x學習之urilib.request簡單學習
參考鏈接:https://docs.python.org/3/library/urllib.request.html#module-urllib.request (翻譯有誤之處請見諒,我還是個初學者. ...
- 遊戲是這樣寫成的 (第三篇: 簡單的遊戲框架)
遊戲是這樣寫成的 (第三篇: 簡單的遊戲框架) 通過上一篇, 我們已有個基本的畫圖功能, 這次讓我們弄一個簡單的遊戲框架吧! 其實一般的遊戲, 大至有兩個主要的函數就可以了: update 和 ren ...
- Csharp windowform bindingNavigator,bindingSource,DataGridView簡單分頁:首頁,上一頁,下一頁,末頁...
/// <summary>/// 塗聚文 2011-10-24 (參考相關網絡和書藉資料)/// 締友計算機信息技術有限公司///C# Winform 簡單分頁: 首頁,上一頁,下一頁,末 ...
- FMDB與SQLite 數據庫應用示範:打做一隻簡單的電影資料庫 App
原文:http://www.appcoda.com/fmdb-sqlite-database/ 作者:GABRIEL THEODOROPOULOS 譯者:kmyhy 通常在 App 中使用數據庫并處理 ...
最新文章
- [转载]IPMSG(飞鸽传书)协议翻译
- 有兴趣的执行一下这段代码
- 前沿科技 | 中科院科学家研究揭示灵活行动选择的神经机制
- php自己写配置项,创建配置文件 用PHP写出自己的BLOG系统 2
- Python基础1 历史 变量
- argparse--解析命令行选项、用法以及说明
- 十三、“词短情长书不尽,桃花潭水是我心。”(2021.2.12)
- 人脸Pose检测:ASM、AAM、CLM方法总结
- 01数字基带信号及其频谱特性
- padding 后尺寸变化 设置_padding margin border 和元素大小
- Spring Boot读取peoperties配置及@Value和@ConfigurationProperties区别和联系
- 为什么Siri总是像个智障?智能助手背后的技术到底有多难?
- 计算机关机键 自动重启,电脑按了关机键后电脑又自动重启了,怎么处理 啊
- 网络分析shell脚本(实时流量+连接统计)
- 34. Element textContent 属性
- WebForm中 页面传参的总结
- Spring MVC-学习笔记(4)数据绑定流程
- 最新获取展示B站UP主实时信息PHP源码分享
- 海明码计算(校验码)
- python: SHA256算法的实现和消息的哈希散列值计算