Networking\InitCmsBundle\Twig\Extension\NetworkingHelperExtension::excerpt PHP Метод

excerpt() публичный Метод

Extracts an excerpt from the text surrounding the phrase with a number of characters on each side determined by radius.
public excerpt ( Twig_Environment $env, string $text, string $phrase, integer $radius = 100, string $ellipsis = '...' ) : string
$env Twig_Environment
$text string String to search the phrase in
$phrase string Phrase that will be searched for
$radius integer The amount of characters that will be returned on each side of the founded phrase
$ellipsis string Ending that will be appended
Результат string
    public function excerpt(\Twig_Environment $env, $text, $phrase, $radius = 100, $ellipsis = '...')
    {
        if (empty($text) || empty($phrase)) {
            return $this->truncate($env, $text, $radius * 2, $ellipsis);
        }
        $text = html_entity_decode($text, null, $env->getCharset());
        $append = $prepend = $ellipsis;
        $phraseLen = mb_strlen($phrase);
        $textLen = mb_strlen($text);
        $pos = mb_strpos(mb_strtolower($text, $env->getCharset()), mb_strtolower($phrase, $env->getCharset()));
        if ($pos === false) {
            return mb_substr($text, 0, $radius, $env->getCharset()) . $ellipsis;
        }
        $startPos = $pos - $radius;
        if ($startPos <= 0) {
            $startPos = 0;
            $prepend = '';
        }
        $endPos = $pos + $phraseLen + $radius;
        if ($endPos >= $textLen) {
            $endPos = $textLen;
            $append = '';
        }
        $excerpt = mb_substr($text, $startPos, $endPos - $startPos, $env->getCharset());
        $excerpt = $prepend . $excerpt . $append;
        return $excerpt;
    }