This will probably turn out to be a simple PHP issue, but it was triggered by a date module upgrade so I'm posting it here for starters. (PHP 4.4.6, mysql-4.1.22, cck-4.7.x-1.4, views-4.7.x-1.5)
Short version:
How do you detect an empty string with non-zero length? (whatever that is)
Long version:
I have a view that returns nodes of 2 types, one with a date field that includes the time (named datetime) and another with a date field that does not include the time (named date). In the theme I use something like the following to determine which one to display ...
if ($field_datetime_value) {
$date = $field_datetime_value;
}
else {
$date = $field_date_value;
}
This worked fine with date-4.7.x-1.2, but since I upgraded to date-4.7.x-1.5 it always treats $field_datetime_value as TRUE and I can't for the life of me figure out what is going on. For cases where $field_date_value was previously empty, it is now a non-empty string of length 44 with no content ... what is that?!
Here's an example, generated by the code below ...
$field_datetime_value = ''
empty($field_datetime_value) =
strlen($field_datetime_value) = 41
strcmp($field_datetime_value, "") = 41
serialize($field_datetime_value) = s:41:"";
preg_match("/\s/", $field_datetime_value) = 1
preg_match("/\d/", $field_datetime_value) = 0
print '$field_datetime_value = ' . "'$field_datetime_value'\n";
print 'empty($field_datetime_value) = ' . empty($field_datetime_value) . "\n";
print 'strlen($field_datetime_value) = ' . strlen($field_datetime_value) . "\n";
print 'strcmp($field_datetime_value, "") = ' . strcmp($field_datetime_value, "") . "\n";
print 'serialize($field_datetime_value) = ' . serialize($field_datetime_value) . "\n";
print 'preg_match("/\s/", $field_datetime_value) = ' . preg_match("/\s/", $field_datetime_value) . "\n";
print 'preg_match("/\d/", $field_datetime_value) = ' . preg_match("/\d/", $field_datetime_value) . "\n";
Even for nodes where the datetime field is defined, PHP thinks the string is 44 characters longer than the content of the string.
$field_datetime_value = 'Sat, Apr 14, 2007 9:30 am'
empty($field_datetime_value) =
strlen($field_datetime_value) = 66
strcmp($field_datetime_value, "") = 66
serialize($field_datetime_value) = s:66:"Sat, Apr 14, 2007 9:30 am";
preg_match("/\s/", $field_datetime_value) = 1
preg_match("/\d/", $field_datetime_value) = 1
So what is this beast and what is the recommended way of detecting when it is empty? Obviously, I can used preg_match("/\d/", $field_datetime_value), but that is certainly a hack, no?
Comments
Comment #1
pwolanin commentedHave you tried using trim()?
Comment #2
RayZ commentedHave you tried using trim()?
Yes. If I do ...
$field_datetime_value = trim($field_datetime_value);... I get exactly the same thing. The output of all my debugging code above is identical. Notice that, strangely, it does match a '\s' regex, even after trim! What is this beast?!
Btw, this is repeatable for me on Linux w/PHP 4.4.2 and OS X w/PHP 4.4.6.
Comment #3
RayZ commentedAargh! Now I feel like an idiot ...
I obviously need a better method of debugging my PHP (better than sticking print statements between
<pre>tags and looking at the output in my browser. For whatever reason it never occurred to me to view the source HTML being returned, which revealed ...$field_datetime_value = '<span class="date-display-single"></span>'So I guess what I really want is something like ...