1

I want to loop group of data with PHP, but my problem is I want to replace all semicolons (;) and line breaks enterspace to comma (,). Looping must also start with my comment (<- Record Start) & (record end).

Data:

P;A251213027;142G;3142831233555;10062014:0420;101;3000.00;0.00; (<- Record Start)
050;0;0;0;N;3000.00;0.00;0.00;Y;;;2PH_02
C;LOIVINO;ICIO
C;1AN INT;MALY;MAR;;;MAL
C;GRSE;LPES
C;B;PHILIPPINES;N0203004177;;Y;12112015;12111977;PHILIPPINES;PHILIPPINES
C;Y;09064656319;DO NOT HAVE;DO NOT HAVE;FRIEND;;
C;
C;;;B125 LO259;QU2TY;;PH (<- End Record)

EDIT I Found out where the record must start and end.

Here's my code

// break each result set
$parts = explode('C;', $str); 

foreach($parts as $part){
    // C;;; is not in the $parts array, if you want it in the result
    // add it here
    $res = explode(';', $part); 
    foreach($parts as $part){
        // you have each line of the result
    }

}
4
  • Try using - str_replace( ';' , ',' , $var) Commented Sep 17, 2016 at 3:41
  • I don't get your requirements. You have multiple C; in there, why do some "end the loop" and some don't? Commented Sep 17, 2016 at 4:22
  • You have edited your sample second time. Are you sure this is what you want and did you yet try the samples provided by the posters? Commented Sep 18, 2016 at 3:24
  • @AT-2016 sorry about that, but this the last edited that im going to make. please see edit above. Commented Sep 20, 2016 at 15:07

2 Answers 2

1

Looking at your data, you don't want to explode on C; but on C;+linebreak. I don't know how wellformed your data is, so let's play it safe: remove spaces, search for all kinds of linebreaks and even something like

C;1;2;3;4;5;6;7
C;   <- end_loop
C;   <- end_loop
C;1;2;3;4;5;US

First be sure there is a linebreak at the end of the data, and add some kind of end of data sign (in this case the underscore _):

$str.= "\r\n_";

Now remove all spaces. And as I don't know what kind of linebreaks you use, replace all kinds of them by some other, easily recognizable character, like * and make sure there are no double linebreaks:

$str = str_replace (' ', '', $str);  //remove all spaces
$str = str_replace (array('\r\n', '\n', '\r'), '*', $str); //replace all linebreaks
$str = str_replace ('**', '*', $str); //remove double linebreaks

then explode on C;*, being the end of a data-part:

$str= explode( 'C;*' , $str );

now for each part you can replace the ; and the * with a comma:

foreach($str as $v){
    $new_str[] = str_replace (array( '*', ';' ), ',', $v) . 'C,';
    }

One last thing to do as you will end up with an unwanted _C, at the end of your array:

end($new_str);
$key = key($new_str);
if($new_str[$key]=='_C,'){
    unset($new_str[$key]);
    }

Why the _ at the end of the file? To make sure you can seperate parts with only C; in the middle of your file, and the last one at the end of the file.

[edit]

Because the actual data differs from the sample given first, a revised answer.

$str.="\r\n_";
$str = str_replace (' ', '', $str);
$str = str_replace (array("\r\n", "\n", "\r"), '*', $str);
$str = str_replace ('**', '*', $str);
$str = str_replace ('C;*', '~~', $str);
$str = str_replace (';*', ';', $str);
$str = str_replace ('*', ';', $str);

$str= explode( '~~' , $str );
foreach($str as $v){
   $new_str[] = str_replace (array( '*', ';' ), ',', $v) . 'C,';
   }

end($new_str);
$key = key($new_str);
if($new_str[$key]=='_C,'){
    unset($new_str[$key]);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

The actual data differs from the sample you gave. I've edited my answer, it works finenow and gives the desired output.
sorry about multiple edited, but i promise this is the last. Please check where the record start and ends. thanks!
1

Try preg_replace() function something like this.

//$str your string with semicolons (;) and new line charecter
preg_replace('/;\s+/', ',', trim($str));

Comments

Your Answer

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