Question

hello i have i problem echo content from a file.

What i want is parse and echo a file and change line whether detect a specific string(e.g \t:\t) in it.

Here is my current code


$full_file_name_exif_txt = "downloaded2/PisaTower/$entry.exif.txt";

if (file_exists($full_file_name_exif_txt) )

 {

   $file = @fopen($full_file_name_exif_txt, "r");

   while (!feof($file)) 

       {

         if(strpos(file_get_contents("downloaded2/PisaTower/$entry.exif.txt"), " : ")) 

          {

            echo "I found \t:\t and i changed line <br/>";

          }

       }

       fclose($file)

       echo file_get_contents("downloaded2/PisaTower/$entry.exif.txt");

      }

my text file contents


IPTC:CodedCharacterSet : UTF8 IPTC:ApplicationRecordVersion : 105 IFD1:Compression : JPEG (old-style) IFD1:ResolutionUnit : inches IFD1:ThumbnailOffset

expectet text file output


IPTC:CodedCharacterSet

: UTF8 IPTC:ApplicationRecordVersion

: 105 IFD1:Compression

: JPEG (old-style) IFD1:ResolutionUni

t: inches IFD1:ThumbnailOffset

Thanks in advance!

Was it helpful?

Solution

I would suggest changing file_exists() to is_file and I'd suggest moving your file_get_contents to just after that, and put the value in a variable like

$contents=file_get_contents("downloaded2/PisaTower/$entry.exif.txt");

That way you don't have to keep reading from the disk (or network)

Then, I'd suggest

$lines=explode(' : ',$contents);
foreach($lines as $line)
{
   echo $line."\n<br>";
}

Notice that I used double quotes " at the end of the echo. That allows the newline \n to be parsed.

OTHER TIPS

Do you look at the result with a browser (opposed to in a shell console) ? The carriage return et tab are not displayed in HTML (replaced by a unique blank space).

The nl2br() function may be of some use in this case.

And you will have to replace \t by some &nbsp; html entities or some css decoration.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top