0

I am writing a code for display only ASCII value.I am reading a file that contain string I want when any NON-ASCII value in file discard whole string and when Ascii value display all content from file.please help me.thank you my code is

$myFile="mydata.txt";
$fh = fopen($myFile, 'r');
$completeString = fread($fh, filesize($myFile));
fclose($fh);

preg_match('/[^\x20-\x7f]/', $completeString);

if(preg_match('/[^\x20-\x7f]/', $completeString) == "1") {
    $completeString = "";
}
elseif(preg_match('/[^\x20-\x7f]/', $completeString) == "0") {
     $completeString ;
}
kuldeep.kamboj
  • 2,566
  • 3
  • 26
  • 63
akki
  • 55
  • 7

1 Answers1

0

A bit of cleanup (not tested):

$myFile="mydata.txt";
$fh = fopen($myFile, 'r');
$completeString = fread($fh, filesize($myFile));
fclose($fh);

if (preg_match('/[^\x20-\x7f]/', $completeString)) {
    $completeString = "";
}

If any characters outside the range x20..x7F are seen, $completeString is emptied out. Now, if this is a text file, it will have certain control characters in it. At a minimum, there will be "end of line" markers, such as "newline" x0A (Linux, etc.), "carriage return-linefeed" x0D0A (DOS, Windows), and x0D in older Macs. Tabs (\x09) are common. You might want to test for \x00 through \x7F.

I'm sure there are other possible approaches.

Phil Perry
  • 2,126
  • 14
  • 18