I want to limit the size of characters that appear, but I can not. Use o:
<? echo $row["catname"] ?>
It takes a text from phpMyAdmin and it shows, but I want to limit the display of characters on the page, how do I?
I want to limit the size of characters that appear, but I can not. Use o:
<? echo $row["catname"] ?>
It takes a text from phpMyAdmin and it shows, but I want to limit the display of characters on the page, how do I?
You can use substr () , this function has 3 parameters.
The string is the input you have, the start is the starting position and the end is the final position.substr (string, start, end);
In case of the second or third parameter: being negative, it counts positions from the end. If it is positive, count from the beginning.
Examples:
echo substr("abcdef", 0, 2); // ab
echo substr("abcdef", 0, 4); // abcd
echo substr("abcdef", 0, -2); // abcd
In cases where it is necessary for an indicator that string
has been truncated,
there is another option (which is almost unknown), which would be the mb_strimwidth
function.
It looks like it has already been made for this purpose:
mb_strimwidth("Hello World", 0, 10, "...");
Result
"Hello W ..."
In this case, you have to note that you have to add the limiter number added to the number of characters that will indicate the limitation.
For example:
mb_strimwidth("Oi mundo", 0, 5, "...")
Displays:
"Hi ..."
The most universal solution is this:
mb_substr ( string $str , int $start , [ int $length [, string $encoding ]] )
The function substr
is limited to single byte encodings, and will have problems with strings with special characters in encodings such as 'UTF-8' (for ISO works well). The mb_substr
counts by characters, not , 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 usually be omitted, in cases where the application is globally configured to use a specific encoding (which is desirable).
More details in the manual:
Related:
Do this:
<?php
echo substr($row["catname"], 0, 20);//Apenas os primeiros 20 caracteres