S
S
Sergey Pugovkin2017-06-04 14:44:57
PHP
Sergey Pugovkin, 2017-06-04 14:44:57

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;
    }
}

BUT: this does not take into account the case where the escape character itself is escaped. For example, given the following file to read:
qwerty\\
asdfgh

Expectation:
[
    "qwerty\\",
    "asdfgh",
]

Reality:
[
    "qwerty\
asdfgh",
]

How to solve the problem?
I don't have any ideas other than character-by-character reading of the file. But this, I suspect, will be very slow. Then it is better to read the file in pieces through fread, then read it character by character in RAM, and, after the found unescaped translation character, discard the "excess".

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
ilyale, 2018-03-23
@ilyale

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 question

Ask a Question

731 491 924 answers to any question