在codeigniter中使用空格限制字符串长度

时间:2015-01-14 06:08:32

标签: php codeigniter

我正在使用codeigniter ..我有2000个字符串的字符串长度,但我想只显示200个字符的字符串,带有空格&如果有一个少于200个字符的字符串,它应该取高度&宽度为200个字符,即在字符串名称后面添加空格。

请帮忙。

3 个答案:

答案 0 :(得分:2)

然后你可以使用以下代码

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

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

答案 1 :(得分:0)

首先检查字符串长度:

$length = strlen($string);

然后检查它是否超过200个字符。如果是这样,请剪切它,否则添加空格:

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

答案 2 :(得分:0)

首先检查字符串的长度,如果它小于它使用str_repeat添加字符,如果它小于仅使用substr使字符串为前200个字符。使用下面的代码

 $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

希望这有助于你