搜索字符串和返回行PHP [英] Search String and Return Line PHP
本文介绍了搜索字符串和返回行PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在PHP文件中搜索字符串,当找到该字符串时,我想返回该字符串所在的整个LINE.这是我的示例代码.我在想我必须使用explode,但无法弄清楚.
I'm trying to search a PHP file for a string and when that string is found I want to return the whole LINE that the string is on. Here is my example code. I'm thinking I would have to use explode but cannot figure that out.
$searchterm = $_GET['q'];
$homepage = file_get_contents('forms.php');
if(strpos($homepage, "$searchterm") !== false)
{
echo "FOUND";
//OUTPUT THE LINE
}else{
echo "NOTFOUND";
}
推荐答案
只需使用 file
函数.
Just read the whole file as array of lines using file
function.
function getLineWithString($fileName, $str) {
$lines = file($fileName);
foreach ($lines as $lineNumber => $line) {
if (strpos($line, $str) !== false) {
return $line;
}
}
return -1;
}
这篇关于搜索字符串和返回行PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文