如何使用PHP Stripos在txt中搜索字符串
我有一个包含 数字,零,一 第二,第三 数字,四,五 在一个名为data.txt的文件中 我想搜索号码,并尝试了这个,但似乎不起作用如何使用PHP Stripos在txt中搜索字符串,php,Php,我有一个包含 数字,零,一 第二,第三 数字,四,五 在一个名为data.txt的文件中 我想搜索号码,并尝试了这个,但似乎不起作用 $file = 'domain.com/data.txt'; $searchnum = 'zero'; if (stripos($file, $searchnum) == true) { echo 'number found' } 更新1.0 我也试过了,但它似乎无法提取txt文件中的数据 $file = "domain.com/data.txt";
$file = 'domain.com/data.txt';
$searchnum = 'zero';
if (stripos($file, $searchnum) == true) { echo 'number found' }
更新1.0
我也试过了,但它似乎无法提取txt文件中的数据
$file = "domain.com/data.txt";
$searchnum = "zero";
if(exec('grep '.escapeshellarg($searchnum).' '.$file )) {
echo "record found";
}
else {
echo "record notfound";
}
如果做得正确,只需提取文件内容
$file = file_get_contents('./data.txt'); // you can use a full http address if your server allows it
$searchnum = 'zero';
if (stripos($file, $searchnum) !== false) { echo 'number found'; }
stripos()
返回搜索字符串的索引,如果未找到,则返回FALSE
因此,如果(stripos($file,$searchnum)!=false){echo'number found';},您将执行
==之所以使用code>,是因为您需要区分false
和0
您没有搜索文件内容。您正在搜索路径字符串。你需要使用file\u get\u contents
或其他东西来打开文件。忘记使用某些东西应属于打字错误;这是真的吗?之后你会想要!==false
而不是==true
,尽管它可以处理示例数据。“似乎不起作用”没有用处。你得到了什么?你期望得到什么?当然,如果你在'domain.com/data.txt'
中搜索'zero'
,你将找不到它。尝试访问并搜索它。注意:http可能不可供他们使用。声明如果fopen包装器已启用,则URL可以用作此功能的文件名。
编辑:此评论与原始帖子相同$file=file\u get\u contents('domain.com/data.txt'),但我仍然没有得到任何结果。有什么想法吗?@JeVic,是的,如果你使用的是域名,那么它必须包括协议,比如:http://thedomain.com/data.txt
很抱歉,我这么做时stack被阻塞了这里是确切的代码@JeVic,change$file=http://yourdomain.com/data.txt';代码>至$file=file\u获取内容('http://yourdomain.com/data.txt');代码>