Search code examples
phpregexpreg-match-allexplode

Backslash causing problems when parsing UTF8 text


I used windows cmd dir /s command to get a list of all pdf files.
Now I want to parse the text and create a simple table that I can copy paste to Excel (after some more text parsing is done).
Just to explain why I don't want to do this in Excel, I need to use the levenshtein function to unifrom/group similar items. But that is not part of the question, I can do that later myself.

My first attempt was regex.

$re = '/(\d{4})\\(\d{2})\\(\d{2})\\(.+?)\\(\d+)-(.+?)\\(.+?) -/m';
$re = '/(\d{4}).(\d{2}).(\d{2}).(.+?).(\d+)-(.+?)\\\\(.+?) -/m';

Non of them works when I run them on 3v4l but on on regex101 the first one works and the second one a simplified version where dot replaces a backslash.
But unfortunatly I can't parse the last bit without a backslash.

My second attempt was simple explode on backslash But that didn't work

$arr = explode("\n", $str);

foreach($arr as $line){
    $parts = explode('\\', $line);
    var_dump($parts);
}

https://3v4l.org/JZ8gR
Because the backslash is used as an escape (I think) in the string.
So I tried to replace the backslash with dash.

$arr = explode("\n", str_replace("\\", "-", $str));
var_dump($arr);/*

https://3v4l.org/Xcs0G
But yet again my text finds a way to beat me.

Full text can be found in any of the links above. A smaler example:

H:\Dokument\Avvikelser\2018\08\03\ALIMENTOS DEL MEDITERRANE\243715000-Vattenmelon\Kvalitets fel - avvikelse27210.pdf
H:\Dokument\Avvikelser\2018\08\06\GRÖNSAKSMÄSTARNA SVERIGE\000233003-Kålrötter 6kg RB\Kvalitets fel - avvikelse27245.pdf
H:\Dokument\Avvikelser\2018\08\06\GRÖNSAKSMÄSTARNA SVERIGE\000223005-Isbergssall. påse RB\Kvalitets fel - avvikelse27244.pdf
H:\Dokument\Avvikelser\2018\08\06\GRÖNSAKSMÄSTARNA SVERIGE\223005000-Isberg påse RB\Kvalitets fel - avvikelse27272.pdf
H:\Dokument\Avvikelser\2018\08\06\TERRA NATURA INTERNATIONA\277711000-Tomat kvist 5kg\ - avvikelse27270.pdf
H:\Dokument\Avvikelser\2018\08\06\TERRA NATURA INTERNATIONA\277711000-Tomat kvist 5kg\Kvalitets fel - avvikelse27270.pdf
H:\Dokument\Avvikelser\2018\08\06\LCT i Skåne\221715000-Ingefära 5kg\Kvalitets fel - avvikelse27279.pdf

What I expect is a each line parsed in such a way that the backslash is not causing issues.
Example:

["H:", "Dokument", "Avvikelser",", "2018", "08", "06", "LCT i Skåne", "221715000", "Ingefära 5kg", "Kvalitets fel", "avvikelse27279.pdf"]

But as the regex implies, I don't need all parts of the string.

["2018", "08", "06", "LCT i Skåne", "221715000", "Ingefära 5kg", "Kvalitets fel"]

is enough.

EDIT: I'm fine with using EOD or " or any other way to initiate the string. But since a ' is used in the text that can't be used.


Solution

  • Use Nowdoc like this, surrounding the "END WORD" with single quotes :

    $str = <<<'EOD'
    H:\Dokument\Avvikelser\2018\08\03\ALIMENTOS DEL MEDITERRANE\243715000-Vattenmelon\Kvalitets fel - avvikelse27210.pdf
    H:\Dokument\Avvikelser\2018\08\06\GRÖNSAKSMÄSTARNA SVERIGE\000233003-Kålrötter 6kg RB\Kvalitets fel - avvikelse27245.pdf
    H:\Dokument\Avvikelser\2018\08\06\GRÖNSAKSMÄSTARNA SVERIGE\000223005-Isbergssall. påse RB\Kvalitets fel - avvikelse27244.pdf
    H:\Dokument\Avvikelser\2018\08\06\GRÖNSAKSMÄSTARNA SVERIGE\223005000-Isberg påse RB\Kvalitets fel - avvikelse27272.pdf
    H:\Dokument\Avvikelser\2018\08\06\TERRA NATURA INTERNATIONA\277711000-Tomat kvist 5kg\ - avvikelse27270.pdf
    H:\Dokument\Avvikelser\2018\08\06\TERRA NATURA INTERNATIONA\277711000-Tomat kvist 5kg\Kvalitets fel - avvikelse27270.pdf
    H:\Dokument\Avvikelser\2018\08\06\LCT i Skåne\221715000-Ingefära 5kg\Kvalitets fel - avvikelse27279.pdf
    EOD;
    
    $re = '/(\d{4})\\\\(\d{2})\\\\(\d{2})\\\\(.+?)\\\\(\d+)-(.+?)\\\\(.+?) -/m';
    $res = preg_match($re, $str, $m);
    
    print_r($m);