There is a txt file containing information in rows. Approximately here:

  00915DU000D1IH5UP ~ 1 ~ 6000037024 ~ 01 ~~ 000 ~ 22617962291 ~ 23510372601 ~ 44000.00 ~ 44000.00 ~ 00074 ~ 23510000900335086001 ~ 03.10.2011 ~ 00634 ~

And you need to break down each landmark into arrays (~)! explode("~",$stroka); (~)! explode("~",$stroka); , but it happens 2(~~) . How can I do this, help!

  • And what about ~~ ? - chernomyrdin
  • Hmm ... Why do you need to do something with ~~? Perhaps this is an empty value of some parameter, and if you skip it, you may experience problems when parsing an array. - ling
  • UTB separators between numbers! don't matter! I want something like: array [0] => 1212122 [1] => 232455 Well, you understand - chambo
  • What array should be obtained from line 6000037024~01~~000~22617962291 ? - chernomyrdin
  • if two ~~ then between them should be an empty cell - chambo

4 answers 4

Method one:

 preg_match_all('/[^~]+/',$string,$matches); 

// $ matches [0] contains the necessary elements.

  • here, too, why is the error: Warning: preg_match_all () expects parameter 2, be string, array given in - chambo
  • Well, $ string is your string. $ matches is an empty variable in which it will add answers. How do you apply? - knes
  • preg_match_all ('/ [^ ~] + /', $ stroka, $ matches); - chambo
  • need to cycle ?? - chambo
  • Well, if you have this array of strings - yes. explode also does not really work with arrays, it seems like. - knes
 print_r(preg_split('/~+/', $str)); 
  • here says that the parameters are not enough - chambo
  • php -r '$str="00915DU000D1IH5UP~1~6000037024~01~~000~22617962291"; print_r(preg_split("/~+/", $str));' Array ( [0] => 00915DU000D1IH5UP [1] => 1 [2] => 6000037024 [3] => 01 [4] => 000 [5] => 22617962291 ) php -r '$str="00915DU000D1IH5UP~1~6000037024~01~~000~22617962291"; print_r(preg_split("/~+/", $str));' Array ( [0] => 00915DU000D1IH5UP [1] => 1 [2] => 6000037024 [3] => 01 [4] => 000 [5] => 22617962291 ) and I have everything ok ... - Alex Kapustin
 explode("~", preg_replace("/~{2,}/", "~", $stroka)) 
  • only it should not be replaced with an empty line, but ~ otherwise the doubles will not turn into single ones, but will disappear altogether - knes
  • all disappeared) - chambo
  • Strangely, this is what I get: var_dump (explode ("~", preg_replace ("/ ~ {2,} /", "", "00915DU000D1IH5UP ~ 1 ~ 6000037024 ~ 01 ~~ 000 ~ 22617962291"))); We get: array (5) {[0] => string (17) "00915DU000D1IH5UP" [1] => string (1) "1" [2] => string (10) "6000037024" [3] => string ( 5) "01000" [4] => string (11) "22617962291"} - chernomyrdin
  • Thanks a lot! you are right! This is where I made a mistake.) And if there can be a space in the line for one ~ 00915DU000D1IH5UP ~ 1 ~ 6000037024 ~ 01 ~~ 000 22617962291 how can I do that so if there is a space, do I need another ?? - chambo
  • @chernomyrdin, [3] is incorrect. There should be [3] => 01, [4] => 000, and you have it stuck together. About what I said. - knes

Thanks a lot! you are right! This is where I made a mistake.) And if there can be a space in the line for one ~ 00915DU000D1IH5UP ~ 1 ~ 6000037024 ~ 01 ~~ 000 22617962291 how can I do that so if there is a space, do I need another ??

  • HATE THIS EDITOR !!! preg_match_all ('/ [^ ~ \ s] + /', $ string, $ matches); // if the space is also a separator Oh, so it worked. =) - knes
  • I do not understand about the gap! See the numbers 00915DU000D1IH5UP ~ 1 ~ 6000037024 ~ 01 ~ 2200017662291 if there is a gap, then I need to do something else! Help plizz - chambo
  • Guys here if two ~~ then this is an empty field what to do help !!!! let the array stay empty! write plizz - chambo
  • one
    If completely different, use a different regular expression. =) \s - space. If ~~ is an empty field, then explode is sufficient. It will turn 123 ~ 456 ~~ 789 into an array (123,456, '', 789); - knes
  • Yes! thanks a lot guys! explode and all) - chambo