Hello,

I am relatively new to PHP and need some help parsing a file.

I was able to use explode to parse the file at the | delimiter , however I cannot figure out how to parse the file at two distinct delimiters.

The file will need to be parsed at the | delimiter and also at the ^ delimiter.

The code below works great, but now I need to expand it to include parsing at ^ delimiter.

<?php
  
  Include "configdatabase.inc";
  
  $filepath = "smp1.txt";
  
  $lines = file($filepath);
  
  $linescount = count($lines);
  
  $data = array();
  
  $i = 0;
  
  foreach ($lines as $val) {
  
      $data[$i] = explode("|", $val);
      
      $i++;
  }
?>

Thanks
JBD

What are the two used for, or are they used to delimit the same things?

What are the two used for, or are they used to delimit the same things?

The two delimiters are not used for anything in particular. They are essentially junk. If I can parse the | and the ^ out of the file, put the contents into an array, then I will be able to post the data to a database.

Thanks for the help
JBD

heh, you're not making sense. At first you're saying | and ^ are both delimiters but now they're junk and can be thrown away? What I'm trying to get at is that if | is used to delimit strings and ^ are used to delimit fields then its a different solution than if they both delimit fields.

If they're truly junk and you want to ignore them, you could replace them with nothing. The code below will remove all carats from the string.

str_replace("^", "", $val);

heh, you're not making sense. At first you're saying | and ^ are both delimiters but now they're junk and can be thrown away? What I'm trying to get at is that if | is used to delimit strings and ^ are used to delimit fields then its a different solution than if they both delimit fields.

My apologies for not including more detail.

All together there are 15 segments named "Part". Within each segment there can be up to 42 sequences. Each sequence is separated by the | delimiter. Within each sequence there can be any number of sequence pieces. In the example below, the name Kim Terry is separated by ^. I would like to break up each segment at | and also at ^.

The original code I include broke up the file at the | delimiter and I was able to get the information I needed from

echo "test:".$data1[1][1]."<br />";
echo "test:".$data1[1][2]."<br />";
echo "test:".$data1[1][3]."<br />";

Below is an example of the file.


Part|3|Kim^Terry|EMC^Emergency Contact^HL70063|3712 Greystone Blvd^^Austin^TX^73301|222-222-2222||||||||

Part|4|Roberts^Amy|OTH^Other^HL70063||333-333-3333|||||||||||||||||||||||||


Thanks again for the help.
JBD

Ok, so they serve the same purpose. Just replace explode("|", $val) with preg_split('/[\|\^]/', $val) and it should work. If it doesn't try replacing \| with just | .

Ok, so they serve the same purpose. Just replace explode("|", $val) with preg_split('/[\|\^]/', $val) and it should work. If it doesn't try replacing \| with just | .

Thanks for the preg_split function. Not sure if the code worked. How would I display the output of the preg split?

Before I used

echo "Test: ".$data[2][1]."<br /><br />".$data[2][2]."<br /><br />".$data[2][3]."<br /><br />".$data[2][5];

Display using explode:
1530
Jones^Robert^Taylor^^
19500515000000M123 Cherry Lane^^Norman^OK^73019405-322-1234^^^bjones@gmail.com405-321-5283M203-12-1234Oklahoma City, OK

After using the preg_split function, I tried

echo  "Test: ".$data[2][5][0];

Display using preg_split:
1530
J
Taylor195005150000002106-373019

Thanks
JBD

Post your new code, its easier than guessing what you changed.

Post your new code, its easier than guessing what you changed.

New code:

<?php
  $filepath = "smp1.txt";
  
  $lines = file($filepath);
  
  $linescount = count($lines);
  
  $data = array();
  
  $i = 0;
  
foreach ($lines as $val){

    //$data[$i] = explode("|", $val);
    $data[$i] = preg_split('/[|\^]/', $val); 
    
    $i++;
    
}  

    echo"MSH: ".$data[0][0]."<br /><br />";
  
  echo "EVN:".$data[1][1]."&nbsp;".$data[1][2]."&nbsp;".$data[1][3]."&nbsp;".$data[1][4].$data[1][5].$data[1][6]."<br /><br /><br /><br /><br />";
    
  echo 
"PID: ".$data[2][1]."<br /><br />".$data[2][2]."<br /><br />".$data[2][3]."<br /><br />".$data[2][5]."<br /><br />".$data[2][7].$data[2][8].$data[2][11].$data[2][13].$data[2][14].$data[2][16].$data[2][19].$data[2][23]."<br /><br /><br /><br /><br />";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.