2

I get a String like this:

'1': '-' '10': CustomerA; 79,00 EUR/Std '11': CustomerB; 155,00 EUR/Std '12': CustomerC; 75,00 EUR/Std '13': CustomerD; 50,00 EUR/Std '2': CustomerA; 000 EUR/Std '3': CustomerA; 000 EUR/Std '4': CustomerA; 120,00 EUR/Std '5': Customer; 75,00 EUR/Std

Better view:

'1': '-'
'10': CustomerA; 79,00 EUR/Std
'11': CustomerB;  155,00 EUR/Std
'12': CustomerC; 75,00 EUR/Std
'13': CustomerD; 50,00 EUR/Std
'2': CustomerA; 000 EUR/Std

And try to get an array with this datas:

array(
'key' => <CustomerName>; <Price> EUR/Std,
...);

I had already tried this, but it does only work if there are word wraps..

$string = preg_split('[0-9+]', $string);
$pattern = "~^'(\d+)':\s+(.+)$~mx";

preg_match_all($pattern, $string, $matches);
$result = array_combine($matches[1], $matches[2]);

Can you help me to solve this problem?

2 Answers 2

2

You can use this pattern without splitting string:

~
'\d+':      # one-or-more digits wrapped by single quotes, followed by colon
\s+         # one-or-more spaces
([^']+)     # group 1: one-or-more not-single quote char
;           # semicolon
\s+         # one-or-more spaces
(.+?)       # group 2: one-or-more character, ungreedy option
\s+         # one-or-more spaces
(EUR/\w+)   # group 3: literally “EUR/” followed by one-or-more word chars
~x

regex101 demo

The groups are:

  1. Customer
  2. Price
  3. Currency

In group 1 I use any not-quote char to avoid 1st empty '1:' match.


Variants:

  1. If you have more Currency variants, replace (EUR/\w+) with (\w+/\w+)
  2. If the /Std is optional, replace (EUR/\w+) with (EUR(/\w+)?) or (\w+(/\w+)?)
Sign up to request clarification or add additional context in comments.

4 Comments

What if price is not always EUR/std?
@Phate01 I have intended all in EUR (as the example provided). But I will add a variant. Thank you.
I know but very often there are always some other case we might not know
@Phate01 Yup. But this is right for any question. We have to suppose that the provided sample is representative of real code... However, I well know that often it is not so. OP question seems sufficiently accurate (He provide also 000 example)
0

Thank you for the competent answer. I modify your $pattern a litl bit to this:

~'(\d+)+':([^']+;\s+.+?)\s+(EUR/\w+)~x

I just need the Id and the customer Company with the price. Then i use a explode function.

2 Comments

Are you sure about this way? You can already have separated values: why double the job?
I need foreach record 3 values: id, name of company and float price. EUR/Std is not necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.