[英]Searching contents of a file using keywords inside array : PHP
I have 3 keywords inside array $val[1]
:eg one, two, three
我有阵列内3个关键字
$val[1]
例如one, two, three
and the following code: 和以下代码:
foreach ($bus as $val){
$val = preg_split('/-./', $val, -1, PREG_SPLIT_NO_EMPTY); // split by _
$val[1] = trim(preg_replace('/\s*\(.*/', '', $val[1])); // remove () if found
$val[1] = trim(preg_replace("/\s*\.\.\.*/", '', $val[1])); // remove ... if found
$pattern = "/.*$val[1].*/i";
$data3 = file_get_contents("foo.txt");
preg_match_all($pattern, $data3, $matches);
foreach ($matches[0] as $v){
echo $pattern."<BR>".$v."<BR>";} }
However when I do the last echo $pattern
in the loop, I found out that it only prints out /.*two.*/i
(meaning it's using only the second keyword to search the file) as opposed to outside of it where it is able to print all keywords 但是,当我在循环中执行最后一个
echo $pattern
时,我发现它只打印出/.*two.*/i
(这意味着它仅使用第二个关键字来搜索文件),而不是在文件外面能够打印所有关键字
/.*one.*/i
/.*two.*/i
/.*three.*/i
Where did I go wrong in the code? 代码在哪里出错? (I'll only be getting 3 lines of text as the result as each keyword will only return one result)
(我只会得到3行文本,因为每个关键字只会返回一个结果)
EDIT: Think i left out an important part of the code, I've edited it to show it more accurately - so I dont think I've been overwriting $val 编辑:认为我遗漏了代码的重要部分,我对其进行了编辑以使其更准确地显示-所以我不认为我已经覆盖了$ val
Examples of what $bus will print $ bus将打印的示例
Ayer Rajah Avenue-.Opp JVC Electron...
Ayer Rajah Avenue-.JVC Electronics ...
Portsdown Road-.Opp Portsdown Camp ...
You are over writing your $val array! 您已经写完$ val数组了!
foreach($val[1] as $keyword) //when you do "as $val" that means for the 2nd time.. $val[1] does not exist!
{
$pattern = "/.*$keyword.*/i"; ...
You want to use something like: 您想使用类似:
foreach ($val as $keyword) {
// replace usage of $val with $keyword
You are taking the second element of the array, $val[1]
, which is two
, and then applying a foreach
against two
. 您正在获取数组的第二个元素
$val[1]
,它是two
,然后对two
应用一个foreach
。 Then you are storing it right back on top of your original variable, $val
. 然后,将其存储在原始变量
$val
顶部。
Edit 编辑
You are only working with $val[1]
. 您仅使用
$val[1]
。 Don't you need to apply all of your logic to all of the entries in $val
? 您是否不需要将所有逻辑应用于
$val
所有条目?
$val = preg_split('/-./', $val, -1, PREG_SPLIT_NO_EMPTY); // split by _
for ($i = 0; $i < count($val); $i++) {
$val[$i] = trim(preg_replace('/\s*\(.*/', '', $val[$i])); // remove () if found
$val[$i] = trim(preg_replace("/\s*\.\.\.*/", '', $val[$i])); // remove ... if found
$pattern = "/.*$val[$i].*/i";
// ...
}
Try using different names for your array and the element in your foreach statement. 尝试为数组和foreach语句中的元素使用不同的名称。
Also I'd recommend you put file_get_contents outside your loop for efficiency reasons. 另外,出于效率考虑,我建议您将file_get_contents放在循环之外。
Finally, your last foreach loop should uses $matches, not $matches[0] (which just calls the first match. 最后,您的最后一个foreach循环应使用$ matches,而不是$ matches [0](它仅调用第一个匹配项)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.