I have an html file, ISO-8859-1, I handle its content as an array and I would like to find empty rows (empty values), but no matter what I do, the value always evaluates to non-empty, however I don't see any content in some of the rows. I think maybe it's because the different encoding, or maybe because it's a different encoding AND an array at the same time, but I can't find what is wrong. if I change encoding in notepad++, empty rows remain still empty. if I turn on show all hidden characters, there is only CRLF. is it possible that it doesn't evaluate empty because of CRLF??
$content = file($filename);
1. row: <tr ...
2. row:
3. row: <tr> ...
foreach ($content as $key => $value) {
if (!empty($value)) {
echo 'not empty!';
}
}
everywhere is echoed 'not empty!' in row nr. 2 also (what seems to be empty). in fact, I wasn't even able to search in this array (with array_search). what is happening here? thanks a lot!
Use
$content = file($filename, FILE_IGNORE_NEW_LINES);
so you do not get the newlines. Or if you want to have only the not-empty lines, use
$content = file($filename, FILE_SKIP_EMPTY_LINES);
Check the documentation for isset()
:
Determine if a variable is set and is not NULL.
Your variable $value
exists but is an empty string and not null
.
Try empty()
instead or just use trim()
, because an empty string will also evaluate to false
:
foreach ($content as $key => $value) {
if (trim($value)) {
echo 'not empty!';
}
}