Skip to content

Instantly share code, notes, and snippets.

@greatsami
Created August 10, 2018 10:59
Show Gist options
  • Select an option

  • Save greatsami/39bed74be1fd566e84dd3fd4ad22e678 to your computer and use it in GitHub Desktop.

Select an option

Save greatsami/39bed74be1fd566e84dd3fd4ad22e678 to your computer and use it in GitHub Desktop.

Revisions

  1. greatsami created this gist Aug 10, 2018.
    13 changes: 13 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    <?php
    function word_limiter($str, $limit = 100, $end_char = '&#8230;'){
    $str2 = strip_tags($str);
    //$str2 = $str;
    if (trim($str2) == ''){
    return $str2;
    }
    preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str2, $matches);
    if (strlen($str2) == strlen($matches[0])){
    $end_char = '';
    }
    return rtrim($matches[0]).$end_char;
    }