Karakter (x) bir miktar sonra metni kesmek

5 Cevap php

Bu, (emin bir fark yapar) wordpress olduğunu

Php Bu bit yazı başlığı çıktılar

<?php echo $data['nameofpost']; ?>

Her yerde uzun 100 karakter kadar olabilir basit bir metin bulunuyor. Ne istiyorum hiç bir şey sadece çıkılır karakter görüntülemek için uzun 20 yaşın üzerinde iseniz '...' ya.

Teşekkürler

5 Cevap

Eğer strlen kullanmak substr ile dize uzunluğunu kontrol ettikten sonra

$string = "This is a large text for demonstrations purposes";
if(strlen($string) > 20) $string = substr($string, 0, 20).'...';
echo $string;

Çıkışlar

"This is a large text..."

Bir sözcüğün sonundaki dize kesmek için başka bir yolu bir regex ile. Bu bir 100 karakter veya 100 karakterden sonra en yakın sözcük Devre kesmek için ayarlanır:

function firstXChars($string, $chars = 100)
{
    preg_match('/^.{0,' . $chars. '}(?:.*?)\b/iu', $string, $matches);
    return $matches[0];
}

in your theme file use something like this try using <div class="teaser-text"><?php the_content_limit(100, ''); ?></div>

sonra functions.php dosyalarında, bu kullanın

function the_content_limit($max_char, $more_link_text = '(more...)', $stripteaser = 0, $more_file = '')
 {

    $content = get_the_content($more_link_text, $stripteaser, $more_file);
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]&gt;', $content);
    $content = strip_tags($content);

   if (strlen($_GET['p']) > 0) 
{

      echo "<div>";
      echo $content;
      echo "</div>";
   }
   else if ((strlen($content)>$max_char) && ($espacio = strpos($content, " ", $max_char )))
 {

        $content = substr($content, 0, $espacio);
        $content = $content;
        echo "<div>";
        echo $content;
        echo "...";
        echo "</div>";
   }
   else {
      echo "<div>";
      echo $content;
      echo "</div>";
   }
}

iyi şanslar :)

<?php
    function abbreviate($text, $max) {
        if (strlen($text)<=$max)
            return $text;
        return substr($text, 0, $max-3).'...';
    }
?>

<?php echo htmlspecialchars(abbreviate($data['nameofpost'], 20)); ?>

Ortak bir gelişme bir sözcüğün sonundaki dize kesmek için denemek olacaktır:

        if (strlen($text)<=$max)
            return $text;
        $ix= strrpos($text, ' ', $max-2);
        if ($ix===FALSE)
            $text= substr($text, 0, $max-3);
        else
            $text= substr($text, 0, $ix);
        return $text.'...';

UTF-8 dizeleri kullanıyorsanız daha uygun karakterleri saymak dize op mb_ multibyte sürümlerini kullanmak isterim.

if(count($data['nameofpost']) > 20)
{
    echo(substr($data['nameofpost'], 0, 17)."...");
}

Bu çıkışı ilk 17 artı üç nokta olacak 20 karakterden sonra $data['nameofpost'] üstü için ....