문제

I need to resize the titles I use this functions but it works like this : car... for... sale 1991 TOYOTA 1.6 16 VALF I want to limit words with 15 likes this:car for sale 1... Here is my Code :

function title_resize($text, $max_length = 15, $fulltext = false)
{
    global $db;
    (string) $display_output = null;

    $text = $db->add_special_chars($text);

    if ($fulltext)
    {
        $output = (strlen($text) > $max_length) ? substr($text, 0, $max_length - 3) . '... '  : $text;
    }
    else 
    {
        $text_words = explode(' ', $text);

        $nb_words = count($text_words);

        for ($i=0; $i<$nb_words; $i++)
        {
            $display_output[] = (strlen($text_words[$i]) > $max_length) ? substr($text_words[$i], 0, $max_length-3) . '... ' : $text_words[$i];
        }

        $output = $db->implode_array($display_output, ' ', true, '');
    }

    return $output;
}

How can i do this ? what I want is limit title with 15 words and display 3 dots I want to do Like This : (this item is fo...) usac : <?=title_resize($item_details['name']);?>

도움이 되었습니까?

해결책

Maybe I'm not getting the question but is this the expected result?

function title_resize($text, $max_length = 15, $fulltext = false)
{
    if ($fulltext)
    {
        return $text;
    }
    else 
    {
        return substr($text, 0, $max_length) . '...';   
    }
}

$str = "this item is for sale";

var_dump(title_resize($str, 10));

다른 팁

function title_resize($title, $length){
    return substr($title, 0, $length) . '...';
}

Try this.... It will show only dots when text greater than specified length.

<?php
function title_resize($text, $length = ''){
    $length = (empty($length)) ? 15 :    $length;
    $New_title  =   substr($text, 0, $length);
    if(strlen($text) > $length)
    {
        $New_title  .= "...";
    }
    return $New_title;
}
echo title_resize("this will only show fifteen characters");
?>

Thanks friends I did something like this one : it works but I am afraid if I loose something in original cod ?

 function title_resize($text, $max_length = 15, $fulltext = false)
{
    global $db;
    (string) $display_output = null;
     $text = $db->add_special_chars($text);
     if ($fulltext)
    {
    $output = $text;        
    }
    else 
    {
        $output = (strlen($text) > $max_length) ? substr($text, 0, $max_length) . '... '  : $text;

    }
    return $output;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top