Skip to Content
Menu

string_limit_chars()

String limiter by number of characters.

PHP August 30, 2017

Usage

PHP
string_limit_chars($string, $charlimit, $continue)

Parameters

$string
(Required) (String) The string to limit
Default: None

$char_limit
(Required) (Integer) The number of characters to limit to
Default: None

Request or provide clarification »

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /libs/Utilities/Utilities.php on line 746.

    1 Hook

    Find these filters and actions in the source code below to hook into them. Use do_action() and add_filter() in your functions file or plugin.

    Filters
    "pre_string_limit_chars"
    Need a new filter hook? Request one here.

    Actions
    This function has no action hooks available. Request one?

    PHP
            public function string_limit_chars($string, $char_limit){
                $override = apply_filters('pre_string_limit_chars', null, $string, $char_limit);
                if ( isset($override) ){return $override;}
    
                $limited['text'] = trim(strip_tags($string));
                $limited['is_limited'] = false;
    
                if ( strlen($limited['text']) <= $char_limit ){
                    return $limited;
                }
    
                $limited['text'] = substr($limited['text'], 0, ($char_limit+1));
                $limited['is_limited'] = true;
    
                return $limited;
            }
    

    Override

    To override this PHP function, use this hook in your child theme or plugin ("my_custom" can be changed):

    PHP
    add_filter('pre_string_limit_chars', 'my_custom_string_limit_chars', 10, 3); //The last integer must be 1 more than the actual parameters
    function my_custom_string_limit_chars($null, $string, $char_limit){ //$null is required, but can be ignored
        //Write your own code here
    
        return true; //Return true to prevent the original function from running afterwords
    }

    You can completely disable this PHP function with a single line actions:

    PHP
     add_filter('pre_string_limit_chars', '__return_false');