From PHP guides to small marketing. From ePayments all the way through to BioTech. Your all-tech passport.
Jun 4, 2012
Php! Limit string charachter length
Hello..
This is my first tutorial and i would like to say thanks to all of you who subscribed to my twitter account.
In this tutorial im gonna show you how to limit string using php.
I know most of want to do that because for example take wordpress or blogger, they have a leetle preview of thir post description on pages.
To do that we gonna use substr function.
We can do an check if the string length is bigger than (for example 10) we will limit it to 5.
Lets move on to real coding:
<?php
// the string
$string = "abcdefghijklmnopqrs...";
// if string is bigger than 10 charachters
if(strlen($string >= 10){
// limit it to 5
$string = substr($string, 0, 5);
}
?>
For more on substr you can read at php official website.
Subscribe to:
Post Comments (Atom)
if(strlen($string >= 10){
ReplyDeleteeshte gabim.
if(strlen($string) >= 10){
eshte e sakte
pastaj pse nese eshte me e gjate se 10 ta limitosh ne 5? nese do qe ta limitosh ne 5, vure kushtin >=5
@anonymous po nje gabim i castit, ate 5 e kam ven sa per example
ReplyDeletemund ta beja dhe == 5 e ta limitoja ne 5 chars 0-5 0,1,2,3,4 :)
Parse error: syntax error, unexpected '{'
ReplyDeleteChange: if(strlen($string >= 10){
To: if(strlen($string >= 10)) {
Sorry, the right one is:
ReplyDeleteif(strlen($string) >= 10){