douhan8610 2018-12-29 13:48
浏览 106
已采纳

PHP注意:未初始化的字符串偏移量

I have an error in my code it says PHP Notice: Uninitialized string offset: 14 on line 9

This is line 9:

while($url[$i]!='/' && $url[$i]!='?' && $i<$len)

What to do to avoid this?

Here is the full code in case you need it:

 function remove_extra_in_url($url)
{
    $extra=array('https://','http://','www.',' ');
    $url=strtolower($url);
    $url=str_replace($extra,'',$url);
    $i=0;
    $site_name='';
    $len=strlen($url);
    while($url[$i]!='/' && $url[$i]!='?' && $i<$len)
    {
        $site_name.=$url[$i];
        $i++;
    }
    return $site_name;
}

Many thanks in advance!

  • 写回答

1条回答 默认 最新

  • dongwei3120 2018-12-29 13:51
    关注

    At the last character, it will be checking the character before checking there is a character to test. Moving $i<$len will stop any further checks once this condition is false due to using &&...

    while($i<$len && $url[$i]!='/' && $url[$i]!='?')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码