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.

4 comments:

  1. if(strlen($string >= 10){
    eshte 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

    ReplyDelete
  2. @anonymous po nje gabim i castit, ate 5 e kam ven sa per example
    mund ta beja dhe == 5 e ta limitoja ne 5 chars 0-5 0,1,2,3,4 :)

    ReplyDelete
  3. Parse error: syntax error, unexpected '{'

    Change: if(strlen($string >= 10){
    To: if(strlen($string >= 10)) {

    ReplyDelete
  4. Sorry, the right one is:
    if(strlen($string) >= 10){

    ReplyDelete