[英]Php strpos does not work correctly when searching a file
我有一個 php 文件,它將在我的服務器上的所有文件中搜索包含字符串的任何文件。 如果我在 strpos() 中對搜索詞進行硬編碼,它可以正常工作。 但如果我嘗試使用 $string 它不會工作
works:
$file = file_get_contents($dir);
if(strpos($file, 'find me'))
does not work:
$findme='find me';
$file = file_get_contents($dir);
if(strpos($file, $findme))
有任何想法嗎?
編輯: if(strpos($file, 'find me').== false) 不能讓它工作。
已解決:找到我一定是抽動症 if(strpos($file, '$findme'))
您應該測試if(strpos($file, 'find me') !== false)
,因為如果文件以“find me”開頭, strpos($file, 'find me')
可能為 0
好吧,我首先想到的是您使用strpos
不正確; 你需要做if(strpos($file, 'find me') !== false)
和if(strpos($file, $findme) !== false)
。
文檔中的警告提到您應該尋找一個明確的false
,而不僅僅是將 function 置於條件中。 因為strpos
可能會返回0
或""
(兩者都評估為假)條件本身可能會失敗。
請嘗試以下操作:
if(strpos($file,$findme) !== false)
此外,如果您說您已經嘗試過但仍然失敗,請嘗試運行var_dump($file)
以確保干草堆確實包含可以搜索的信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.