Answer the question
In order to leave comments, you need to log in
How to read a file line by line, given that the newline character can be escaped, including the escape character itself?
I did like this:
protected function getFields(string $filePath): \Generator
{
$file = new SplFileObject($filePath);
$line = '';
foreach ($file as $buffer) {
if (substr($buffer, -2) == "\\\n") {
$line .= substr($buffer, 0, -2) . "\n";
continue;
}
yield $line;
}
}
qwerty\\
asdfgh
[
"qwerty\\",
"asdfgh",
]
[
"qwerty\
asdfgh",
]
Answer the question
In order to leave comments, you need to log in
the newline character cannot be escaped. it either exists or it doesn't, it's a system symbol.
I recommend reading the file line by line. Once you've started parsing the file as an object, continue using getTargetType , for example . The method will return you a string already without the newline character.
Next, split the string using separators and the Explode
function.
Then you can "combine" the resulting array elements by getting rid of slashes and other garbage, using, for example, array_walk or something else
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question