Limit text display in PHP

Asked

Viewed 30,006 times

7

I want to limit the size of characters that appear, but I’m not getting it. I use the:

<? echo $row["catname"] ?>

It takes a text from phpMyAdmin and shows, only I want to limit the display of characters on the page, as I do?

4 answers

16

For cases where an indicator is required that the string has been truncated, there is another option (which is almost unknown), which would be the function mb_strimwidth.

It seems she was already made for that purpose:

mb_strimwidth("Hello World", 0, 10, "...");

Upshot

"Hello W..."

In this case, you should note that you have to add the limiter number added to the number of characters that will be the limitation indication.

For example:

 mb_strimwidth("Oi mundo", 0, 5, "...")

Displays:

"Hi..."

  • Note: As for the character quantifier, I have the impression that I read somewhere that this is a bug.

  • But in that case, the rest of the text 'dies' or it remains there only hidden ?

  • The text "dies" yes.

10

You can use the replace(), this function has 3 parameters.

substr(string, start, end);

To string is the input you have, the beginning is the initial position and the end is the final position.

In the case of the second or third parameter: if negative, it counts positions from the end. If positive, it counts from the beginning.

Examples:

echo substr("abcdef", 0, 2); // ab
echo substr("abcdef", 0, 4); // abcd
echo substr("abcdef", 0, -2); // abcd
  • How can I use this in <? echo $Row["catname"] ?> ?

  • 1

    @user7329: <?php echo substr($row["catname"], 0, 2); ?> just need to adjust with the length you want.

  • 1

    Very good! @Sergio

7

The most universal solution is this:

mb_substr ( string $str , int $start , [ int $length [, string $encoding ]] )

The function substr is limited to encodings single byte, and will have problems with strings with special characters in encodings like 'UTF-8' (for ISO it works well). Already the mb_substr account by characters, and not bytes, being more suitable for portable code.

Use with ISO-8859-1:

echo mb_substr( 'Acentuação faz diferença', 4, 10, 'ISO-8859-1' );

Use with UTF-8:

echo mb_substr( 'Acentuação faz diferença', 4, 10, 'UTF-8' );

Important: the last parameter can normally be omitted, for cases where the application is globally configured to use a specific encoding (which is desirable).

More details in the manual:

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

Related:

How to show only "x" characters in a div?

I cannot limit text with Japanese characters

  • Your friend! You saw my comment in the other answer, right! + 1

  • 1

    @Wallacemaxters Vc indicates duplicate, comments and does not respond? : P

  • 1

    Won’t you link to your other answer? I think "relate" the questions would be nice :)

  • 1

    @Bacco is not due to PHP, it is because of the CSS part that Wallace cited http://answall.com/a/117962/3635

6

Do so:

<?php
echo substr($row["catname"], 0, 20);//Apenas os primeiros 20 caracteres

Browser other questions tagged

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