MODx truncate function

Создаем сниппет с именем phx:character_limit

<?php
if (strlen($output) > $options) {
  $output_cutted = substr($output, 0, $options);
  $last_space = strrpos($output_cutted, " ");
  $output = substr($output_cutted, 0, $last_space) . " ...";
        preg_match_all('#<([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $output, $result);
    $openedtags = $result[1];

    #put all closed tags into an array
    preg_match_all('#</([a-z]+)>#iU', $output, $result);
    $closedtags = $result[1];
    $len_opened = count($openedtags);

    # all tags are closed
    if (count($closedtags) == $len_opened) {
        return $output;
    }
    $openedtags = array_reverse($openedtags);

    # close tags
    for ($i=0; $i < $len_opened; $i++) {
        if (!in_array($openedtags[$i], $closedtags)){
            $output .= '</'.$openedtags[$i].'>';
        } else {
            unset($closedtags[array_search($openedtags[$i], $closedtags)]);
        }
    }
  }
return $output;
?>

Пример вызова:

[+content:character_limit=`300`+]

Поделиться с друзьями

Френдинг

 

Comments

No comments so far.

Leave a Reply

 
(will not be published)
 
 
Comment