[英]How can I use Regex in a While Loop in PHP?
I've to do the Regex in a While loop bc its to much data to do it in once.我必须在 While 循环中执行 Regex,因为它需要大量数据才能一次性完成。 By now I can go throw the Data and the regex also works but the Data get not stored.
现在我可以去扔数据,正则表达式也可以工作,但数据没有被存储。 So How can I do this?
那么我该怎么做呢?
This is my code now:这是我现在的代码:
$handle = @fopen($PathToFile, "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
if (preg_match_all('/^\w+\s+\d+\s('. $SelectedTime .':\d+.\d+).\d+.\d+\s(.+)/im', $buffer, $matches, PREG_SET_ORDER)) {
$decodeData[] .= $matches;
}
else {
}
}
var_dump($decodeData);
}
fclose($handle);
For help I would be really glad如需帮助,我会很高兴
This is wrong syntax, it is not a string concatenation.这是错误的语法,它不是字符串连接。
Instead of代替
$decodeData[] .= $matches;
Output:输出:
$decodeData[0] = "Result1Result2Result3Result4";
$decodeData[0] = "Result1Result2Result3Result4";
Use用
$decodeData[] = $matches;
Output:输出:
$decodeData[0] = "Result1";
$decodeData[0] = "Result1";
$decodeData[1] = "Result2";$decodeData[1] = "Result2";
$decodeData[2] = "Result3";$decodeData[2] = "Result3";
$decodeData[3] = "Result4";$decodeData[3] = "Result4";
You need to use preg_quote
function to escape $SelectedTime
variable correctly and use $decodedData[] = $matches
without .=
.您需要使用
preg_quote
函数来正确转义$SelectedTime
变量并使用$decodedData[] = $matches
而没有.=
。
$handle = @fopen($PathToFile, "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
$pattern = '/^\w+\s+\d+\s('. preg_quote($SelectedTime) .':\d+.\d+).\d+.\d+\s(.+)/im';
if (preg_match_all($pattern, $buffer, $matches, PREG_SET_ORDER)) {
$decodeData[] = $matches; // just assignment operator
}
else {
}
}
var_dump($decodeData);
}
fclose($handle);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.