【发布时间】:2018-02-26 22:59:54
【问题描述】:
我确定我错过了一些东西。我只知道够危险。
在我的 php 代码中,我使用 file_get_contents() 将文件放入变量中。
然后我遍历一个数组并使用 preg_match 多次搜索同一个变量。该文件是一个制表符分隔的 txt 文件。它可以正常运行 800 次,但在中间随机一次它会做一些非常奇怪的事情。
$current = file_get_contents($file);
foreach($blahs as $blah){
$image = 'somefile.jpg';
$pattern = '/https:\/\/www\.example\.com\/media(.*)\/' . preg_quote($image) . '/';
preg_match($pattern, $current, $matches);
echo $matches[0];
}
由于某种原因,它有时会在两个 URL 之间转换一个标签。当我查看 txt 文件时,首先列出了我要查找的图像,然后是第二个 iamge,但 echo $matches[0] 以相反的顺序返回它。它不像 echo $matches[0] 返回它那样存在。就像您搜索字符串“一二”并且 $matches 返回“二一”一样。
【问题讨论】:
-
“我知道够危险” - 成就了我的一天
-
请显示您的输入并指出导致问题的行。
-
$matches[0] will contain the text that matched the full pattern -
.匹配除换行符以外的所有字符。这包括选项卡。这可能是你的问题吗?
标签: php regex preg-match