Plugin index
<?php
if (class_exists('\Textpattern\Tag\Registry')) {
Txp::get('\Textpattern\Tag\Registry')->register('rvm_substr');
}
function rvm_substr($atts, $thing)
{
extract(lAtts(array(
'length' => 10,
), $atts));
assert_int($length);
$thing = parse($thing);
$parsed = preg_split('/(<[^>]*>)/', $thing, -1, PREG_SPLIT_DELIM_CAPTURE);
$ellipsis = FALSE;
$istag = FALSE;
$tags = array();
$total = 0;
$out = '';
foreach ($parsed as $chunk)
{
if ($total === $length)
{
$ellipsis = TRUE;
break;
}
if ($istag)
{
if (substr($chunk, 1, 1) === '/')
{
array_pop($tags);
}
elseif (substr($chunk, -2, 1) !== '/')
{
array_push($tags, $chunk);
}
$out .= $chunk;
}
else
{
if (trim($chunk) !== '')
{
$len = preg_match_all('/(?:[^&]|&[^;]+;)/us', $chunk, $dummy);
if ($total + $len <= $length)
{
$out .= $chunk;
$total += $len;
}
else
{
preg_match('/^(?:[^&]|&[^;]+;){'.($length-$total).'}/us', $chunk, $match);
$out .= $match[0];
$total = $length;
$ellipsis = TRUE;
}
}
else
{
$out .= $chunk;
}
}
$istag = !$istag;
}
$out = rtrim($out) . ($ellipsis ? '…' : '');
foreach(array_reverse($tags) as $tag)
{
preg_match('/^<(\w+)/s', $tag, $match);
$out .= '</'.$match[1].'>';
}
return $out;
}
?>