I am presently developing a Wordpress theme and that i come with an area which shows the headline as well as an excerpt, nevertheless the excerpt will have to be another length depending how lengthy headline (i.e. the headline and excerpt both occupy a set space). Well, I require a method of dynamically altering the excerpt length for the way lengthy the publish title is.

I have seen two clips of code which might be useful. The very first is given below:

    <?php
    // Variable & intelligent excerpt length.
    function print_excerpt($length) { // Max excerpt length. Length is set in characters
        global $post;
        $text = $post->post_excerpt;
        if ( '' == $text ) {
            $text = get_the_content('');
            $text = apply_filters('the_content', $text);
            $text = str_replace(']]>', ']]>', $text);
        }
        $text = strip_shortcodes($text); // optional, recommended
        $text = strip_tags($text); // use ' $text = strip_tags($text,'<p><a>'); ' if you want to keep some tags

        $text = substr($text,0,$length);
        $excerpt = reverse_strrchr($text, '.', 1);
        if( $excerpt ) {
            echo apply_filters('the_excerpt',$excerpt);
        } else {
            echo apply_filters('the_excerpt',$text);
        }
    }

    // Returns the portion of haystack which goes until the last occurrence of needle
    function reverse_strrchr($haystack, $needle, $trail) {
        return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) + $trail) : false;
    }

This could then simply just be utilized e.g. <?php print_excerpt(50); ?>. The code below changes the excerpt length in line with the title length. Wouldso would Time passes about mixing both of these bits of code?

        <?php
        // Dynamically resize excerpt according to title length
        $rem_len = ""; //clear variable
        $title_len = strlen($post->post_title); //get length of title
        if($title_len <= 35){
            $rem_len=188; //calc space remaining for excerpt
        }elseif($title_len <= 70){
            $rem_len=146;
        }elseif($title_len <= 105){
            $rem_len=104;
        }elseif($title_len <= 140){
            $rem_len=62;
        }
        $trunc_ex = substr($post->post_excerpt, 0, $rem_len); //truncate excerpt to fit remaining space
        if(strlen($trunc_ex) < strlen($post->post_excerpt)) $trunc_ex = $trunc_ex . " [...]";
        echo "<p>" . $trunc_ex . "</p>"; //display excerpt
        ?>

Solved:

<?php
// Variable & intelligent excerpt length.
function print_excerpt($title) { // Max excerpt length. Length is set in characters
global $post;

$rem_len = ""; //clear variable
$title_len = strlen($title); //get length of title
$excerpt_line=40;
if($title_len <= 17){
    $rem_len=$excerpt_line*8; //calc space remaining for excerpt
}elseif($title_len <= 34){
    $rem_len=$excerpt_line*7;
}elseif($title_len <= 51){
    $rem_len=$excerpt_line*6;
}elseif($title_len <= 68){
    $rem_len=$excerpt_line*5;
}elseif($title_len <= 85){
    $rem_len=$excerpt_line*4;
}

$text = $post->post_excerpt;
if ( '' == $text ) {
    $text = get_the_content('');
    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]>', $text);
}
$text = strip_shortcodes($text); // optional, recommended
$text = strip_tags($text,'<p>'); // use ' $text = strip_tags($text,'<p><a>'); ' if you want to keep some tags

$text = substr($text,0,$rem_len);
$excerpt = reverse_strrchr($text, ' ', 1);
if( $excerpt ) {
    echo apply_filters('the_excerpt',$excerpt);
} else {
    echo apply_filters('the_excerpt',$text);
}

}

    // Returns the portion of haystack which goes until the last occurrence of needle
function reverse_strrchr($haystack, $needle, $trail) {
    return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack,     $needle) + $trail) : false;
}
?>