我正在尝试将表单提供的 UTC 时间和表单提供的事件名称字符串与从文件中读取的数组进行匹配。问题是它似乎总是匹配,即使它不应该匹配。文件的格式将始终保持不变,所以我知道我会在双引号内寻找一个值,所以在使用 strpos() 无法获得结果后,我尝试了 preg_match ...现在匹配所有内容。代码和示例输出如下($utc 和 $event_name)在我们到达这里时已经设置并正确):
$match1 = "/\"{$utc}\"/";
$match2 = "/\"{$event_name}\"/";
print "Match Values: $match1, $match2<p>";
foreach($line_array as $key => $value) {
print "Value = $value<p>";
if ((preg_match($match1,$value) == 1) and (preg_match($match2,$value) == 1))
{
print "Case 1 - False<p>";
} else {
print "Contains targets: $value<p>";
//code to act on hit will go here
}
}
这就是返回的内容:
Match Values: /"1371033000000"/, /"Another test - MkII "/
Value = { "date": "1357999200000", "type": "meeting", "title": "Plant and Animal Genome Conference, San Diego, CA", "description": "NCGAS to present at Plant and Animal Genome Conference, San Diego, CA", "url": "http://www.event1.com/" }
Contains targets: { "date": "1357999200000", "type": "meeting", "title": "Plant and Animal Genome Conference, San Diego, CA", "description": "NCGAS to present at Plant and Animal Genome Conference, San Diego, CA", "url": "http://www.event1.com/" }
Value = { "date": "1357693200000", "type": "meeting", "title": "Testing Addition", "description": "This is a fake event.", "url": "http://pti.iu.edu" }
Contains targets: { "date": "1357693200000", "type": "meeting", "title": "Testing Addition", "description": "This is a fake event.", "url": "http://pti.iu.edu" }
Value = { "date": "1371033000000", "type": "meeting", "title": "Another test - MkII", "description": "This is a fake event.", "url": "http://pti.iu.edu" }
Contains targets: { "date": "1371033000000", "type": "meeting", "title": "Another test - MkII", "description": "This is a fake event.", "url": "http://pti.iu.edu" }
我应该只匹配最后一个,但它们都匹配。我一直在玩正则表达式,似乎找不到合适的魔法。