user4423224
user4423224

Reputation:

Limit string length with white spaces in codeigniter

I am using codeigniter.. I have 2000 character of string length but I want to show only 200 characters of string with white spaces & if there is a string with less than 200 characters it should take height & width of 200 characters i.e. white spaces should be added after the string name.

Please help.

Upvotes: 0

Views: 5556

Answers (3)

Utkarsh Dixit
Utkarsh Dixit

Reputation: 4275

Firstly just check the length of the string, if its less than it add the characters using str_repeat and if its less than just make the string to only first 200 char using substr.Use the code below

 $length = strlen($string);
    if($length<200){
    $text . = str_repeat(' ', 200-$length); ;
    str_repeat('&nbsp;', 5);
    }
    else{
    $text = substr($string, 0, 200);
    }
    echo $text; // will print the text max and min to 200

Hope this helps you

Upvotes: 0

Tariq
Tariq

Reputation: 402

then u can use the following code

$str = 'some text';
$length = strlen($str);

if ($length < 200) {
    $str .= str_repeat('&nbsp', 200 - $length);
} else {
    $str = substr($str, 0, 200);
}
echo $str;

Upvotes: 2

Justinas
Justinas

Reputation: 43451

First check string length:

$length = strlen($string);

Then check if it's longer than 200 chars. If so, cut it, else add spaces:

if ($length < 200) {
    $string .= str_repeat(' ', 200 - $length);
} else {
    $string = substr($string, 0, 200);
}

Upvotes: 0

Related Questions