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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题