doushan2224 2015-12-03 16:04
浏览 69

too long

I want to generate specific characters string. I need to generate exact 35 characters string for that I am using this function.

var_dump(str_pad("some text some text", 35, " ", STR_PAD_RIGHT));

So, it's giving me this output:

string(35) "some text some text "

That's exactly fine. It'll add blank spaces for remaining characters.

But, For this type of scenario, I want to discard characters above 35.

var_dump(str_pad("some text some text some text some text some text some text some text some text", 35, " ", STR_PAD_RIGHT));

Current Output:

string(79) "some text some text some text some text some text some text some text some text"

I need it 35 characters here as well by discarding last few characters from string.

  • 写回答

2条回答 默认 最新

  • doukang1962 2015-12-03 16:11
    关注
    if (mb_strlen($string,"UTF-8") > 34){
        //there are 35 or more characters to the string
        $string = mb_substr($string,0,35,"UTF-8");
    }
    else {
        //there are 34 or less characters so carry on
        $string = str_pad($string, 35, " ", STR_PAD_RIGHT));
    }
    

    Using mb_substr to remove the string length exceeding the 35 characters limit. This is the MultiByte variation which I think should be recommended for counting.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题