detect a line break with fgetc

so i am reading data from a file one character at-a-time using the fgetc command. i would like to perform a task when a line break is detected. i am using “\n” at the end of each line to indicate the end of the line. is there a way to allow fgetc to recognize the “\n” as one character instead of two characters? the backslash is one character, and the “n” is another character. i don’t see how fgetc would know to combine the 2 characters into a single line break.

this is the code i am using:

[php]$file=fopen(“filename.txt”,“r”) or exit(“Unable to open file!”);
while(!feof($file))
{
$data = trim(fgetc($file));
if ($data == “\n”) echo ‘new line reached’;
}
fclose($file);
[/php]

of course the script never echos ‘new line reached’ because it can’t find “\n” anywhere in the file. my guess is because it is two characters instead of one character. can anyone help me with this?

I don’t think it’s possible with the fgetc function, all newlines and spaces result in a “0” binary value when it reads it. You’re not going to be able to determine whats a space and whats a newline.

Don’t know what is your end goal but here is what I’ve written. Hope it will do the trick for you.

[php]<?php

$file_name = 'filename.txt';

if (file_exists($file_name)){

	$handle = fopen($file_name,'r');
	
	$string = file_get_contents($file_name);

	$line_count = substr_count($string, '\n');

	for ($i = 1; $i <= $line_count ; $i++) { 

		echo 'New Line Found <br>';
	}
	
	fclose($handle);

}

?>[/php]

thank you for your assistance Topcoder and tanzeelniazi. i think i found a workaround to the issue. i was able to combine the fgets and fgetc commands to my advantage. fgets reads one line of the file and writes it to a second temp file. then fgetc reads the contents of the temp file one character at-a-time. this removes the need to detect when a new line is reached, because there is only one line in the file.

[php]$file1=fopen(“filename.txt”,“r”) or exit(“Unable to open file!”);

while(!feof($file))
{

$data = trim(fgets($file));

$file2=fopen(“temp.txt”,“w”) or exit(“Unable to open file!”);
fputs($file2, $data);
fclose($file2);

$file2=fopen(“temp.txt”,“r”) or exit(“Unable to open file!”);

while(!feof($file))
{
$temp_data = trim(fgetc($file2));
//perform a task with $temp_data
}
fclose($file2);

}
fclose($file1);
[/php]

if there is a better way to do this, advice is always welcome. thanks again!

Once you do the fgets you don’t have a need for the fgetc, just have to combine your code withs @tanzeelniazi example to get this.

[php]
$file1=fopen(“filename.txt”,“r”) or exit(“Unable to open file!”);

while(!feof($file1))
{
$data = trim(fgets($file1));
$line_count = substr_count($string, ‘\n’);

for ($i = 1; $i <= $line_count ; $i++)
{

   if substr($data, $i, 1) = "a" 
  {
      // perform a task 
  }  

}
}
fclose($file1);
[/php]

excellent! that will be a lot easier than dumping the data to 2 files just to run a simple fgetc on one line. thanks again. in case you’re curious, i am changing the key of songs. i finally got it working yesterday. i had to match up each chord with a number. i used an array to “count” between the original chords and the new key chords. not fun, but it works.

this part grabs the data and converts it to numbers:

[php]//converts the current and desired song keys into numbers
$file=fopen(“songs/” . $song_filename,“r”) or exit(“Unable to open file!”);
$null = trim(fgets($file));
$song_key = trim(fgets($file));
fclose($file);

if ($song_key == “Am”) $song_key = “A”;
if ($song_key == “Bm”) $song_key = “B”;
if ($song_key == “Cm”) $song_key = “C”;
if ($song_key == “Dm”) $song_key = “D”;
if ($song_key == “Em”) $song_key = “E”;
if ($song_key == “Fm”) $song_key = “F”;
if ($song_key == “Gm”) $song_key = “G”;

$chords_array = array(“A”,“B”,“C”,“D”,“E”,“F”,“G”,“A”,“B”,“C”,“D”,“E”,“F”,“G”,“A”,“B”,“C”,“D”,“E”,“F”,“G”);

if ($song_key == “A”) $song_key_number = 7;
if ($song_key == “B”) $song_key_number = 8;
if ($song_key == “C”) $song_key_number = 9;
if ($song_key == “D”) $song_key_number = 10;
if ($song_key == “E”) $song_key_number = 11;
if ($song_key == “F”) $song_key_number = 12;
if ($song_key == “G”) $song_key_number = 13;

if ($desired_song_key == “A”) $desired_song_key_number = 7;
if ($desired_song_key == “B”) $desired_song_key_number = 8;
if ($desired_song_key == “C”) $desired_song_key_number = 9;
if ($desired_song_key == “D”) $desired_song_key_number = 10;
if ($desired_song_key == “E”) $desired_song_key_number = 11;
if ($desired_song_key == “F”) $desired_song_key_number = 12;
if ($desired_song_key == “G”) $desired_song_key_number = 13;

if ($song_key_number > $desired_song_key_number) $song_key_shift = $song_key_number - $desired_song_key_number;
if ($song_key_number < $desired_song_key_number) $song_key_shift = $desired_song_key_number - $song_key_number;
if ($song_key_number == $desired_song_key_number) $song_key_shift = 0;[/php]

then i used the array as a counter to move up/down the octave:

[php]if ($song_key_number < $desired_song_key_number)
{
if ($one_character == “A”) $xone_character = $chords_array[7+$song_key_shift];
if ($one_character == “B”) $xone_character = $chords_array[8+$song_key_shift];
if ($one_character == “C”) $xone_character = $chords_array[9+$song_key_shift];
if ($one_character == “D”) $xone_character = $chords_array[10+$song_key_shift];
if ($one_character == “E”) $xone_character = $chords_array[11+$song_key_shift];
if ($one_character == “F”) $xone_character = $chords_array[12+$song_key_shift];
if ($one_character == “G”) $xone_character = $chords_array[13+$song_key_shift];
}

if ($song_key_number > $desired_song_key_number)
{
if ($one_character == “A”) $xone_character = $chords_array[7-$song_key_shift];
if ($one_character == “B”) $xone_character = $chords_array[8-$song_key_shift];
if ($one_character == “C”) $xone_character = $chords_array[9-$song_key_shift];
if ($one_character == “D”) $xone_character = $chords_array[10-$song_key_shift];
if ($one_character == “E”) $xone_character = $chords_array[11-$song_key_shift];
if ($one_character == “F”) $xone_character = $chords_array[12-$song_key_shift];
if ($one_character == “G”) $xone_character = $chords_array[13-$song_key_shift];
}

if ($song_key_number == $desired_song_key_number) $xone_character = $one_character;

if ($one_character == “b”) $xone_character = “b”;
if ($one_character == “m”) $xone_character = “m”;
if ($one_character == “#”) $xone_character = “#”;
if ($one_character == “7”) $xone_character = “7”;
if ($one_character == " ") $xone_character = " ";
if ($one_character == “”) $xone_character = “”;

$song_data .= $xone_character;[/php]

it all comes together in the $song_data variable, which gets dumped to separate files in 24-line chunks, or however many lines the config file specifies:

[php]$presentation_song_filename = $song_filename . " (" . $page_number . " of " . $page_count . “)”;

if (file_exists(“presentation_songs/” . $presentation_song_filename))
{
echo ‘’;
echo ‘’;
echo ‘

’;
echo ‘Whoops! It looks like you already added this song to the presentation:
’;
echo ‘

’;
echo ‘"’ . $song_filename . ‘"’;
echo ‘

’;
echo ‘’;
echo ‘
’;
echo ‘’;
echo ‘Go Back’;
exit;
}

$file2=fopen(“presentation_songs/” . $presentation_song_filename,“w+”) or exit(“Unable to open file!”);
fputs($file2, $slide_count . “\n”);
fputs($file2, $song_key . “\n”);
fputs($file2, $song_speed . “\n”);
fputs($file2, $song_data);
fclose($file2);[/php]

i had some fun with this one!

I have to say that’s pretty cool

Sponsor our Newsletter | Privacy Policy | Terms of Service