PHP Limit letters in result [duplicate]

0

How do I limit the amount of letters that will appear in <?= $v['titulo']?> within <div class="col-md-4 panel">

I have tried several things, but it only makes mistakes.

<?php $result = selectAllAnuncios(1,18); foreach ($result as $k => $v):
    $link = "anuncio,".$v['id'].",".str_replace(" ","-",$v['titulo'])."-".str_replace(" ","-",$v['ano']);
    $image  = (@is_file("img/anuncios/".$v["imgs"][0]["name"])) ? "img/anuncios/".$v["imgs"][0]["name"] : "img/no-img.jpg" ; ?>

    <div class="col-md-4 panel ">
        <a href="<?=$link?>"> <img src='<?=$image?>' width="200" height="100">
        <div ><?=$v['titulo'] ?> </br> <?=$v['ano']?> - R$ <?=$v['preco']?>   
            </a>
        </div>
    </div>

    <?php endforeach; ?>
    
asked by anonymous 06.12.2016 / 11:43

1 answer

0

If you are limiting the characters, you can use substr of PHP itself. In the following way:

<?php $result = selectAllAnuncios(1,18); foreach ($result as $k => $v):
    $link = "anuncio,".$v['id'].",".str_replace(" ","-",$v['titulo'])."-".str_replace(" ","-",$v['ano']);
    $image  = (@is_file("img/anuncios/".$v["imgs"][0]["name"])) ? "img/anuncios/".$v["imgs"][0]["name"] : "img/no-img.jpg" ; ?>

    <div class="col-md-4 panel ">
        <a href="<?=$link?>"> <img src='<?=$image?>' width="200" height="100">
        <div ><?=substr($v['titulo'], 0, 10)."..."; ?> </br> <?=$v['ano']?> - R$ <?=$v['preco']?>   
            </a>
        </div>
    </div>

    <?php endforeach; ?>
substr($v['titulo'], 0, 10)."...";

In this case, 0 is the starting point and 10 is the amount of characters to read.

  

Reference: link

As suggested by Wallace, you can use the m_substr function as follows:

mb_substr($v['titulo'], 0, 10,'UTF-8');

  

Reference: link

    
06.12.2016 / 11:48