PHP Parse file example
PHP Parse file example
Today I’m going to show you a little example of how you can use PHP to parse a file. Well it really depends on the purpose you’re doing this for, but in all cases basically you need to open the file and read it line by line. There are a few methods that can do this, I have used a few myself. In my last script, I had to create a translation file that uses parts from one file combined with parts from another file. This would be the purpose for this script today.
Why did I had to do this? Well I asked a friend to make a translation for me and due to some misunderstanding the translation was made a little bit wrong, not the translation per se, but rather the format the translation was made was a bit wrong.
For example, lets say we have the following original file:
1 2 3 |
# English: Translated Home: Home Contact: Contact |
So this was an original YAML format file and the translation should have been, for instance in German:
1 2 3 |
# English: Translated Home: Startseite Contact: Kontakt |
however the translation was received with:
1 2 3 |
# English: Translated Startseite: Startseite Kontakt: Kontakt |
So unfortunately the YAML format was now wrong.
Solution
So what did I thought? It would have been a fair amount of lines to go over and fix there, where programming can do this so much easier and faster (a few milliseconds!).
So what I did was open the original English file and save the values from the left side of the delimiter “:” in an temporary array and then open the translated file and again save the left or right side in an array (I went with the left side, it doesn’t matter in this case). Of course, when saying open, I mean using php.
Then count both temporary arrays and making sure both have the same number of elements, then parse these temporary arrays and write the final result into a new file.
Here is the code example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<?php $english = file_get_contents('english.txt'); $german = file_get_contents('german.txt'); $english_line_array = explode("\n", $english); $german_line_array = explode("\n", $german); $first_part = array(); $second_part = array(); foreach ($english_line_array as $line) { if(substr($line,0,1) == '#' || $line == '' ) { $first_part[] = $line; continue; } $line_array = explode(': ', $line); $first_part[] = $line_array[0]; } foreach ($german_line_array as $line) { if(substr($line,0,1) == '#' || $line == '' ) { $second_part[] = $line; continue; } $line_array = explode(': ', $line); $second_part[] = $line_array[1]; } $new_german = 'new_german.txt'; $fh = fopen($new_german, 'w') or die("can't open file"); $counter_eng = count($first_part); $counter_ger = count($second_part); if($counter_eng == $counter_ger) { for ($i =0 ; $i < $counter_eng; $i++) { if(substr($first_part[$i],0,1) == '#' || $first_part[$i] == '' ) { fwrite($fh, $first_part[$i] . "\n"); continue; } $new_line = $first_part[$i] . ': '.$second_part[$i]; fwrite($fh, $new_line . "\n"); } } else { echo "Counter is not ok, lines from first file vs second file differ! \n"; } fclose($fh); ?> |
You will see that for lines starting with “#” or blank lines, these will be written from the first array without making any changes to it.
The result would be a new_german.txt file that would contain the proper YAML text format and so could be used as normal:
1 2 3 |
# English: Translated Home: Startseite Contact: Kontakt |
If you have any comments, please bellow.