简体   繁体   English

Php strpos 在搜索文件时无法正常工作

[英]Php strpos does not work correctly when searching a file

I have a php file that will search all the files on my server for any files that contains a string.我有一个 php 文件,它将在我的服务器上的所有文件中搜索包含字符串的任何文件。 If i hard code the search term in the strpos() it works fine.如果我在 strpos() 中对搜索词进行硬编码,它可以正常工作。 but if i try to use a $string it wont work但如果我尝试使用 $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)) 

any ideas?有任何想法吗?

edit: if(strpos($file, 'find me').== false) does not make it work.编辑: if(strpos($file, 'find me').== false) 不能让它工作。

resolved: find me must be in tics if(strpos($file, '$findme'))已解决:找到我一定是抽动症 if(strpos($file, '$findme'))

you should be testing if(strpos($file, 'find me') !== false) , because strpos($file, 'find me') could be 0 if the file starts with "find me"您应该测试if(strpos($file, 'find me') !== false) ,因为如果文件以“find me”开头, strpos($file, 'find me')可能为 0

Well, the first thing that leaps out at me is that you're using strpos incorrectly;好吧,我首先想到的是您使用strpos不正确; you need to do if(strpos($file, 'find me') !== false) and if(strpos($file, $findme) !== false) .你需要做if(strpos($file, 'find me') !== false)if(strpos($file, $findme) !== false)

The warning in the docs mention you should look for an explicit false and not just place the function in a condition. 文档中的警告提到您应该寻找一个明确的false ,而不仅仅是将 function 置于条件中。 Because strpos could potentially return 0 or "" (which both evaluate to false) the condition itself could fail.因为strpos可能会返回0"" (两者都评估为假)条件本身可能会失败。

Try the following instead:请尝试以下操作:

if(strpos($file,$findme) !== false)

Also, if you're saying you've tried this and it's still failing, try running a var_dump($file) to make sure the haystack does indeed contain information that can be searched through.此外,如果您说您已经尝试过但仍然失败,请尝试运行var_dump($file)以确保干草堆确实包含可以搜索的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM