php - is_numeric does not work when reading from txt file? -
in text file have file this:
olivia 7 sophia 8 abigail 9 elizabeth 10 chloe 11 samantha 12
i want print out name , ignore numbers.
for reason, dont work - wouldn't print anything?
<?php $file_handle = fopen("names.txt", "rb"); while (!feof($file_handle) ) { $line_of_text = fgets($file_handle); if (!is_numeric((int)$line_of_text)) { echo $line_of_text; echo "<br />"; } } fclose($file_handle); ?>
you casting every line (int)
. lines strings become 0
(zero).
you can change code to:
!is_numeric($line_of_text)
note: is_numeric() return true
decimals , scientific notation also. if strictly determining if line contains digits, suggest ctype_digit()
update
you need trim($line_of_text)
fgets() includes newline.
code inside while()
:
$line_of_text = trim(fgets($file_handle)); if (!ctype_digit($line_of_text)) { echo $line_of_text; echo "<br />"; }
Comments
Post a Comment