Skip to content
Advertisement

How can i find item number and quantity from this raw text

I would like to select item number and quantity from a text, I’ve been stuck a bit, but have tried something to search for _______ in the text and replace/delete characters, but I’m not really moving forward so hope someone can help me with.

The raw text is:

1 **1197** **3** 1,00KG ROLLER SMOKE SMOKED CA1,5KG 14811692 28.04.20 49 9992 ________ 2 **331 3** 1,00KG SALAMI POTATO CA750G 14563423 30.07.20 49 9992 ________ 3 **443 5** 1 ST OX SALAMI HALAL 200G 14946417 05.05.20 49 9992 ________ 4 **533 2** 1 PK FRANKFURTER 70G/STK 350G 15507358 05.05.20 49 9992 ________ 5 **544 1** 1,00KG SAUSAGE DELI CA1KG 14794445 05.05.20 49 9992 ________

The text may vary, for example, the first character is a line number (which I do not need for anything) which can be 1,2,3 ..or also.. 100,101, etc. so this must be taken into account.

I only need item number and quantity (the bold ones) from the text to use for database search, so my output schould be something like:

1197,3
331,3
443,5
533,2
544,1

Advertisement

Answer

You could take advantage of the explode() function:

<?php

$str = '1 1197 3 1,00KG ROLLER SMOKE SMOKED CA1,5KG 14811692 28.04.20 49 9992 ________ 2 331 3 1,00KG SALAMI POTATO CA750G 14563423 30.07.20 49 9992 ________ 3 443 5 1 ST OX SALAMI HALAL 200G 14946417 05.05.20 49 9992 ________ 4 533 2 1 PK FRANKFURTER 70G/STK 350G 15507358 05.05.20 49 9992 ________ 5 544 1 1,00KG SAUSAGE DELI CA1KG 14794445 05.05.20 49 9992 ________';

$parts = explode('________ ', $str);
foreach($parts as $part) {
    $strings = explode(' ', $part);
    $id = $strings[1];
    $qty = $strings[2];
    echo $id . ',' .$qty."rn";
}
?>

gives:

1197,3
331,3
443,5
533,2
544,1

See here for a live demo.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement