Breaking php text limit

0

Aspicturedabove,newspostingisnotlimitedtocontent.Accordingtophpcodes,itshouldlimitupto70,butgivesmorethan1000characters...

Lookatthecodes...

home.php

<divid="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
?>
    
asked by anonymous 14.03.2017 / 00:13

1 answer

1

The problem is:

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

Let's translate what you're doing:

$limita_str is limiting $str from the first character ( 0 ) to what is defined in $limita which is the second argument of the function, hence% to 0 .

$limita is actually the position of the last space ( $ultimo ) found within .

The $limita_str returns the text of return from $str space ( $ultimo ) to infinity, then returns from $str to infinity.

Did you find 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 $ultimo by default includes this.

This will not actually cut to the last space, but it will not break a character in the middle, ie it will not show these " php-common ", as in ���� , which is not multi-byte. / p>

If you really want to use substr and preserve the function as it 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).'...';
}
    
14.03.2017 / 00:28