Trim string of words based on character count
Yes, it's extremely inefficient, because in every iteration you count the number of words twice: once in count_words, and then wp_trim_words internally has to count the words again.

Another problem is that the method won't work for content that is many words larger than the target limit length. For example with a content of 4000 chars and a limit of 100, due to the if ($count &gt; 10) break check in the middle, the method will basically cut off the last 10 words and return a still very large content. Not sure if that was part of your plan.

I don't really know PHP. But here's a naive implementation that should be more efficient and work with long content too:

function is_letter($char) {
    return ctype_alpha($char) || in_array($char, ['-']);
}

function custom_length_excerpt_max_char($content, $max_chars_limit) {
    if (strlen($content) &lt; $max_chars_limit) {
        return $content;
    }
    $cut_at = $max_chars_limit;
    for (; $cut_at &gt;= 0 &amp;&amp; is_letter($content{$cut_at}); --$cut_at);
    return substr($content, 0, $cut_at);
}

function test_custom_length_excerpt_max_char() {
    $sample = 'some sample-text some sample-text';
    for ($i = 5; $i &lt; strlen($sample) + 10; ++$i) {
        echo custom_length_excerpt_max_char($sample, $i)."\n";
    }
}


Here's the logic behind the main function:


If the content is shorter than the limit, return it
Iterate backward from the target limit position, until you find something that's NOT a letter, and break the content there.


If you run the test function, the output is something like:

...
some
some
some sample-text
some sample-text
some sample-text
some sample-text
some sample-text
some sample-text some
some sample-text some
some sample-text some
...