Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Perch template filter for getting a Perch item's reading length in minutes
<?php
class PerchTemplateFilter_length extends PerchTemplateFilter
{
public function filterBeforeProcessing($value, $valueIsMarkup = false)
{
$word = str_word_count(strip_tags($value));
$m = floor($word / 200);
$s = floor($word % 200 / (200 / 60));
if ($m > 0) {
$est = $m . ' minute' . ($m == 1 ? '' : 's');
} else {
$est = 1 . ' minute';
}
return $est;
}
}
PerchSystem::register_template_filter('length', 'PerchTemplateFilter_length');
@ryangittings

This comment has been minimized.

Copy link
Owner Author

commented May 13, 2019

Usage example:

<perch:blog id="postDescHTML" filter="length">

@muster-mark

This comment has been minimized.

Copy link

commented May 22, 2019

Looks like you intended to use $s before the 's', but didn't.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.