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

时间:2013-08-13 01:21:31

标签: php url

好的,基本上我正在阅读这段源代码并且不了解特定区域的目的。

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;
    }
}

所以基本上从我的理解中,在getURLData方法中使用'array_shift'的两个while循环清空了数组但是根据我的逻辑,第二个while循环甚至不能清空任何东西,因为第一个while循环已经完成了。 / p>

然后是方法的最后一行getURLData

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

做同样的事情,但如果传入的参数已经为空?

很困惑!!!

1 个答案:

答案 0 :(得分:1)

第一个while循环删除数组中字符串长度为零的所有前导元素,第二个循环元素与尾随元素相同。 reset($ array)将指向第一个,结束($ array)到最后一个元素。

为什么他第二次冥想它?我不知道。