Page 1 of 1
count characters in file
Posted: Fri Jan 28, 2005 1:53 pm
by juline
hi friends,
is there a possibility of counting characters in a file like .pdf or .doc ?
cheers,
juline
Posted: Fri Jan 28, 2005 2:02 pm
by John Cartwright
not too sure but maybe
Code: Select all
$contents = file_get_contents($file);
echo strlen($contents);
I know that applies to text, but not sure about documents and such.
Posted: Fri Jan 28, 2005 2:57 pm
by juline
thanx.
i dcripted following code which is uploading a file and counting the chracters.
Code: Select all
<?php
$uploaddir = 'uploads/temp/';
$filename=$_FILESї'file']ї'name'];
if (move_uploaded_file($_FILESї'file']ї'tmp_name'], $uploaddir."$filename"))
{
$datei=fopen("uploads/temp/$filename", r);
$content=file_get_contents("uploads/temp/$filename");
echo "Filename: ";
echo "$filename";
echo "<br>";
echo "Chars ";
echo strlen($content);
}
?>
but the output is not exactly correct. it puts more out than actually is in file. then i would like to devide tha amount of chars into 50. and after displaying the result delete the uploaded file.
can someone help me please ?
i tried the following:
Code: Select all
<?php
$uploaddir = 'uploads/temp/';
$filename=$_FILESї'file']ї'name'];
if (move_uploaded_file($_FILESї'file']ї'tmp_name'], $uploaddir."$filename"))
{
$datei=fopen("uploads/temp/$filename", r);
$content=file_get_contents("uploads/temp/$filename");
$norm=$content/50;
echo "Dateiname: ";
echo "$filename";
echo "<br>";
echo "Zeichen ca. ";
echo strlen($content);
echo " <br>";
echo "Normzeile(n): ";
echo "$norm";
}
?>
regards,
juline
Posted: Fri Jan 28, 2005 3:23 pm
by feyd
why not use filesize()?
Posted: Fri Jan 28, 2005 4:59 pm
by juline
feyd wrote:why not use filesize()?
i dont know what you mean. the characters are being count to calculate the approximate value for the standard lines in the text by deviding into 50.
Posted: Fri Jan 28, 2005 5:09 pm
by feyd
both pdf and doc are binary file formats. You won't be able to just open the file with any arbitrary function and read out all the actual text data.
The code you are working with right now is based off the file's size. Not the character counts contained in their encoding. This is the reason the sizes you recieve back are inaccurate.
Posted: Fri Jan 28, 2005 6:46 pm
by juline
all right, i got it. but i dont know what to do now.