Lets say I have following lines:
How, are you!
Are you there?
Yes, you over there.
I have to display those lines which contains word "there", In my case it should return following array of strings:
["Are you there?", "Yes, you over there."]
What I did is :
$arr = array();
while (!feof($file)) {
$line = fgets($file);
if (strpos($line, $keyword) !== false) {
$arr[]=$line;
}
}
print_r($arr);
return;
How can I get the result in ["Are you there?", "Yes, you over there."]
format.
Here's a quick way:
$result = preg_grep("/$keyword/", file("/path/to/file.txt"));
file()
creates an array of each lineIf you want that literal string, then that is JSON:
$string = json_encode($result);
Regex?
preg_match_all("/.* there .*|there .*|.* there.*/", $input_str, $output_array);
You can replace the "there" with a variable.