dongmu1996 2013-11-08 09:55
浏览 34
已采纳

循环使用ifs和whiles卡住加载(PHP)

I have the following code, which looks perfectly fine logically, but refuses to do anything, and just gets stuck loading until it times out.

Code:

<?php
$name = "billy bobby sharkingflardo herdaderpingtonning";

while (strlen($name) > 17) {
    $nameEx = explode(' ', $name);

    if (strlen($nameEx[0] > 1)) {
        $nameEx[0] = substr($nameEx[0], 0, -(strlen($nameEx[0]) - 1));
    }

    while (count($nameEx) > 2) {
        unset($nameEx[1]);
    }

    if (strlen($nameEx[1] > 15)) {
        substr($nameEx[1], 0, -1);
    }

    $name = implode(' ', $nameEx);
}

echo $name;
?>

I don't have much else to say, the code is needed to shorten names which are too long, it does this by reducing the first name to 1 character and removing any middle names, and if the last name alone is too long with remove a character until it is the right length.

Thanks for any help -Tom

  • 写回答

2条回答 默认 最新

  • duanfeigui6655 2013-11-08 10:11
    关注

    No need to use loops here completely fixed version of your script:

    <?php
    $name = "egergerg ergergerg ergregerg ergergerg dfgeergergergdfsgergergergerg";
    
    if(strlen($name) > 17)
    {
        $nameEx = explode(' ', $name);
    
        if (strlen($nameEx[0]) > 1)
        {
            $nameEx[0] = substr($nameEx[0], 0, -(strlen($nameEx[0]) - 1));
        }
    
        if(count($nameEx) > 2)
        {
            $nameEx = Array($nameEx[0], $nameEx[count($nameEx)-1]);
        }
    
        if(strlen($nameEx[1]) > 15)
        {
            $nameEx[1] = substr($nameEx[1], 0, -(strlen($nameEx[1]) - 15));
        }
    
        $name = implode(' ', $nameEx);
    }
    
    echo $name;
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3