doolo00026 2013-08-13 01:21
浏览 81

用于拆分查询字符串的URL解析器

Ok so basically I am reading through this piece of source code and do not understand the purpose of a specific area.

class URL_Processor
{
    private static $urlPath;
    private static $urlBits = array();

    /*
Gets data from the current URL
@return Void
*/
public function getURLData()
{
    $urldata = (isset($_GET['page'])) ? $_GET['page'] : '' ;
    self::$urlPath = $urldata;
    if( $urldata == '' )
    {
        self::$urlBits[] = 'home';
        self::$urlPath = 'home';
    }
    else
    {
        $data = explode( '/', $urldata );
        while ( !empty( $data ) && strlen( reset( $data ) ) === 0 ) 
        {
            array_shift( $data );
        }
        while ( !empty( $data ) && strlen( end( $data ) ) === 0) 
        {
            array_pop($data);
        }
            self::$urlBits = $this->array_trim( $data );
        }
    }

   private function array_trim( $array ) 
   {
        while ( ! empty( $array ) && strlen( reset( $array ) ) === 0) 
        {
            array_shift( $array );
        }

        while ( !empty( $array ) && strlen( end( $array ) ) === 0) 
        {
            array_pop( $array );
        }

        return $array;
    }
}

So basically from my understanding the two while loops with 'array_shift' in the getURLData method empty out the array but according to my logic the second while loop wont even be able to empty anything out because the first while loop already did.

Then the last line of the method getURLData

self::$urlBits = $this->array_trim( $data );

does the same thing but how if the passed in argument is empty already?

Very confused!!!

  • 写回答

1条回答 默认 最新

  • dpauf28808 2013-08-13 01:29
    关注

    The first while loop removes all leading elements in the array where their string length is zero, the second one does the same with trailing elements. reset($array) will point to the first, end($array) to the last element.

    Why he mushes it through a second time? I don't know.

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊