I don’t understand how to parse the line below into variables for further use.

Analgin usual capsules 300 mg, 20 pcs.

  • $ name = "Analgin"
  • $ type = "normal"
  • $ dosage = "300mg capsules"
  • $ count = "20 pcs"

PS I did not find suitable topics

  • Describe the task more specifically - EatMyDust
  • Apparently still the names will be with different numbers of words? if so, then nothing =) - Ruslan Mirzapulatov
  • If a regular, a solution to the forehead: regex101.com/r/zwLKfa/1 :) - gil9red
  • If you are going to solve this problem in general, then give all possible variants of names. Instead of capsules, life can probably be pills, for example. Perhaps there may be ampoules, then the volume will be in ml and not mg. etc. - teran
  • Little data, provide more examples of strings to be matched - Yuriy Prokopets

2 answers 2

It is possible so: $ a = explode ("", $ str);

  • for some reason it seems to me that with this solution, the капсулы 300мг and 20 шт will be divided into different array elements - teran
  • So it is, every word will be an element of the array. - EatMyDust
  • yes, but nothing interferes, take after two elements of the array, just if there is a more specific task, then describe it - Dmitriy
  • Read the statement of the problem, how do you think your answer and the truth will do what the author asks? - EatMyDust
  • The task contains 1 line, so I ask for more specifics - Dmitriy

Can be parsed with regular expressions

 $str = 'Анальгин обычный капсулы 300 мг, 20 шт.'; preg_match_all('~\pL+(?:\s\d+\s\pL+\S)?|\d+\s\pL+\S~u', $str, $arr); list($name, $type, $dosage, $count) = $arr[0]; echo "$name $type $dosage $count";