Home > Mobile >  extract decimal numbers from string
extract decimal numbers from string

Time:10-13

I would like to extract decimal numbers (at least with 2 decimal places) I tried this:

$myString = 'Rechnung WEEE-Reg.-Nr. DE 89633988, Batterie-Reg.-Nr. 21003868 LU-BIO-04 Amazon EU S.à r.l. - 38 avenue John F. Kennedy, L-1855 Luxembourg Sitz der Gesellschaft: L-1855 Luxemburg eingetragen im Luxemburgischen Handelsregister unter R.C.S. B 101818 • Stammkapital: 37.500 EUR Amazon EU S.à r.l., Niederlassung Deutschland – Marcel-Breuer-Str. 12, D-80807 München, Deutschland Sitz der Zweigniederlassung: München eingetragen im Handelsregister des Amtsgerichts München unter HRB 218574 USt-ID : DE 814584193 Seite 1 von 1 USt. y Zwischensumme (ohne USt.) USt. 19y 47,05 € 8,94 € USt. Gesamt 47,05 € 8,94 € Gesamtpreis 55,99 € Rechnungsdetails Bestelldatum 04 Oktober 2021 Bestellnummer XXXXXX Zahlungsreferenznummer XXXXX Verkauft von Amazon EU S.à r.l., Niederlassung Deutschland USt-IDNr. DE814584193 Rechnungsdatum /Lieferdatum 05 Oktober 2021 Rechnungsnummer XXXXX Zahlbetrag 55,99 € XXXXXX Um unseren Kundenservice zu kontaktieren, besuchen Sie www.amazon.de/contact-us Rechnungsadresse XXXXXXXX Lieferadresse XXXXXXX Verkauft von Amazon EU S.à r.l., Niederlassung Deutschland Marcel-Breuer-Str. 12 80807 München Deutschland USt-IDNr. DE814584193 Bestellinformationen Beschreibung Menge Stückpreis (ohne USt.) USt. y Stückpreis (inkl. USt.) Zwischensumme (inkl. USt.) AVM FRITZ!WLAN Mesh Repeater 1200 (Zwei Funkeinheiten: 5 GHz (bis zu 866 MBit/s), 2,4 GHz (bis zu 400 MBit/s), 1x Gigabit-LAN, deutschsprachige Version) ASIN: B07V3TKDV5 1 47,05 € 19yb 55,99 € 55,99 € Versandkosten 0,00 € 0,00 € 0,00 €'

preg_match_all('!\d \.*\d*!', $myString, $matches);

My Output:

[0] => Array
        (
            [0] => 89633988
            [1] => 21003868
            [2] => 04
            [3] => 38
            [4] => 1855
            [5] => 1855
            [6] => 101818
            [7] => 37.500
            [8] => 12
            [9] => 80807
            [10] => 218574
            [11] => 814584193
            [12] => 1
            [13] => 1
)

The only correct value is 37.500 which I would like to have in my array. Where is my mistake ? :/

** UPDATE **

preg_match_all('!\d \.\d*!', $myString, $matches);

Array
(
    [0] => Array
        (
            [0] => 89633988,
            [1] => 12,
            [2] => 47,05
            [3] => 8,94
            [4] => 47,05
            [5] => 8,94
            [6] => 55,99
            [7] => 55,99
            [8] => 2,4
            [9] => 47,05
            [10] => 55,99
            [11] => 55,99
            [12] => 0,00
            [13] => 0,00
            [14] => 0,00
        )

)

Better but not perfect. 0,1 and 8 are wrong.

CodePudding user response:

Try with this Regex Pattern: \d*(,|\.)\d{2,3}

Output will be

    [0] => Array
        (
            [0] => 37.500
            [1] => 47,05
            [2] => 8,94
            [3] => 47,05
            [4] => 8,94
            [5] => 55,99
            [6] => 55,99
            [7] => 47,05
            [8] => 55,99
            [9] => 55,99
            [10] => 0,00
            [11] => 0,00
            [12] => 0,00
        )

And if you only need "Stammkapital" then use this reges: \d*(,|\.)\d{2,3} EUR output will be:

    [0] => Array
        (
            [0] => 37.500 EUR
        )

    [1] => Array
        (
            [0] => 37.500
        )

CodePudding user response:

If you want to match at least 2 decimal places for positive and negative decimals try the following

/[ -]?([0-9] [\.,][0-9]{2,}|[\.,][0-9]{2,})/

[ -]? matches a leading or a leading - char or none of them before the decimal

([0-9] [\.,][0-9]{2,}|[\.,][0-9]{2,}) group of matching decimals like 1.500 or without a number before the dot like .0001 with 2 or more decimal places regarding . and ,.

CodePudding user response:

I think:

\d*[.,]\d{2,}

achieves what you want, optional leading numbers, . or ,, then 2 or more integers.

https://3v4l.org/MUIOv

Update:

If UK/Europe separators (,) are not allowed:

\d*[.]\d{2,}

should work. (optional leading numbers, ., then 2 or more integers.)

https://3v4l.org/cmoUt

This only provides 37.500 as a result.

  • Related