PHP Limit letters in the result

Asked

Viewed 452 times

0

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

I’ve tried a lot of things, but it’s a mistake.

<?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; ?>
  • You tried to replace him?

  • What would be these mistakes? Exceptions or does not limit the characters? ...

  • You want me to show up like, title here...

1 answer

0


If you are going to limit the characters, you can use the substr of PHP itself. As follows:

<?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 number of characters to be read.

Reference: http://php.net/manual/en/function.substr.php

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

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

Then passing the UTF-8 so there are no problems with special characters.

Reference: http://php.net/manual/en/function.mb-substr.php

  • Ball show, ran round here. I did not know this function.

  • Beauty @Leandroborges :) As soon as possible accept my answer as correct. Note also that you can take a look at the documentation I put as reference, it is important to read.

  • 2

    You’ll have character problems UTF-8 use substr. In that case, you’d better use mb_substr with the parameter UTF-8, to have no problems.

  • 2

    I recommend that you always search if there is already an answer to the problem using the site search engine http://answall.com/search?tab=votes&q=subst%20%5bphp%5d%20is%3aanswer and instead of answering prefer to duplicate.

  • For once, there is another problem in the answer: Any text will always have the "..." at the end. I think it would be interesting to add a check to know the size of the string before limiting it and placing reticence

  • Very valid your argument @Wallacemaxters.

  • 1

    Thanks for the personal help. I took the "..." from the end, put mb_substr to UTF-8 and limited it to 22 characters because of the width of the column. Thank you all.

Show 2 more comments

Browser other questions tagged

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