Skip to content

Instantly share code, notes, and snippets.

@pepeloper
Created March 20, 2017 12:58
Show Gist options
  • Save pepeloper/ea7dffb9567a3ac07705f0016ee4a6ac to your computer and use it in GitHub Desktop.
Save pepeloper/ea7dffb9567a3ac07705f0016ee4a6ac to your computer and use it in GitHub Desktop.

Revisions

  1. pepeloper created this gist Mar 20, 2017.
    28 changes: 28 additions & 0 deletions drupal_trim_string.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    # Drupal Snippet

    In this snippet we will know how to trim any string in Drupal way using the [truncate_utf8](https://api.drupal.org/api/drupal/includes!unicode.inc/function/truncate_utf8/7.x) function

    ## Code

    ```php
    truncate_utf8($string, $max_length, $wordsafe = FALSE, $add_ellipsis = FALSE, $min_wordsafe_length = 1)
    ```
    So for example we can do something like this:

    ```php
    $text = 'Here we have a large text saying nothing';
    truncate_utf8($text, 12);
    // Here we have a l
    ```

    ```php
    $text = 'Here we have a large text saying nothing';
    truncate_utf8($text, 12, TRUE);
    // Here we have a
    ```

    ```php
    $text = 'Here we have a large text saying nothing';
    truncate_utf8($text, 12, TRUE, TRUE);
    // Here we have...
    ```