Break php text limit

Asked

Viewed 279 times

0

inserir a descrição da imagem aqui As image above, the posting of the news is not limited to the content. According to php codes, it should limit up to 70, but gives more than 1000 characters...

Look at the codes...

home php.

<div id="bloco-tres">
    <h1>Outros Posts</h1>
    <?php
        foreach($query->selecaoLimit('4,5') as $outros){
    ?>
    <div class="outro">
        <a href="<?php echo $base.'/'.$outros['categoria'].'/'.$outros['slug'];?>" title="<?php echo $outros['titulo'];?>">
            <img src="posts/<?php echo $outros['exibicao'];?>" width="190" height="108" border="0"/>
            <span><?php echo $outros['titulo'];?></span>
            <p><?php echo $query->limitar($outros['conteudo'],70);?></p>
        </a>
    </div><!-- outro -->
   <?php }?>
</div><!-- termina bloco tres -->

Querys.class.php

<?php
class Querys extends BD{

    public function selecaoLimit($limite){
        $sqlLimite = "SELECT * FROM `posts` WHERE status = '0' AND categoria != 'artigos' ORDER BY id DESC LIMIT $limite";          
        return self::conn()->query($sqlLimite);
    }//método de seleção de dados limitado 


    public function selecaoArtigos($limite, $categoria){
        $sqlLimite = "SELECT * FROM `posts` WHERE status = '0' AND categoria = '$categoria' ORDER BY id DESC LIMIT $limite";            
        return self::conn()->query($sqlLimite);
    }//método de seleção de dados limitado


    public function limitar($str, $limita = 100, $limpar = true){
            if($limpar = true){
                $str = strip_tags($str);    
            }
            if(strlen($str) <= $limita){
                return $str;    
            }
            $limita_str = substr($str, 0, $limita);
            $ultimo = strrpos($limita_str, ' ');
            return substr($str, $ultimo).'...';
        }//TERMINA FUNCÇÃO PARA LIMITAR STRING
?>
  • troque Return substr($str, $ultimo). '...'; for Return substr($limita_str, $ultimo).'...';

1 answer

1


The problem is in:

$limita_str = substr($str, 0, $limita);
$ultimo = strrpos($limita_str, ' ');
return substr($str, $ultimo).'...';

Let’s translate what you’re doing:

The $limita_str is limiting the $str from the very first character (0) up to what is defined in $limita which is the function’s second argument, hence 0 until $limita.

The $ultimo is actually the position of the last space () found inside the $limita_str.

The return returns the text of $str since the $ultimo space () until infinity, soon returns the $str since $ultimo to infinity.


Found the problem?

The problem could simply be solved like this:

public function limitar($str, $limita = 100, $limpar = true){

       if($limpar = true){
           $str = strip_tags($str);    
       }

       return mb_substr($str, 0, $limita).'...';
}

Requires the Multibyte String, I believe that the php-common by default includes this.

This in fact would not cut to the last space, but will not break a character in the middle, ie will not appear these "����", as occurs in the substr, that is not multi-byte.


If you really want to use substr and preserve function as is, use:

public function limitar($str, $limita = 100, $limpar = true){
    if($limpar = true){
        $str = strip_tags($str);
    }
    if(strlen($str) <= $limita){
        return $str;
    }
    $limita_str = substr($str, 0, $limita);
    $ultimo = strrpos($limita_str, ' ');
    return substr($limita_str, 0, $ultimo).'...';
}
  • Solved Buddy! Broke a gallon for me, thank you very much boss!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.