need help on a piece of code from PHP 5 Social Networking -
i'm trying more advanced php , pick book php 5 social networking michael peacock. while book seemed interesting didn't involved in details of code. function i'm trying figure out is,
public function geturldata() { $urldata = ( isset( $_get['page'] ) ) ? $_get['page'] : '' ; $this->urlpath = $urldata; if( $urldata == '' ) { $this->urlbits[] = ''; $this->urlpath = ''; } else { $data = explode( '/', $urldata ); while ( !empty( $data ) && strlen( reset( $data ) ) === 0 ) { //notes: php array_shift — shift element off beginning of array array_shift( $data ); } while ( !empty( $data ) && strlen( end( $data ) ) === 0) { array_pop($data); } $this->urlbits = $this->array_trim( $data ); } }
this part of larger class , $_get['page'] this: relationships/mutual/3. main question happening in else section. think happening it's removing empty array indexes question that.
any appreciated.
edit: added array_trim function part of class
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; }
public function geturldata() {
gets 'page', data can obtained $_get url: instance: http://mysite.com/?page=contact
if 'page' has been set, assigned $urldata, else $urldata=''
$urldata = ( isset( $_get['page'] ) ) ? $_get['page'] : '' ; $this->urlpath = $urldata; if( $urldata == '' ) { $this->urlbits[] = ''; $this->urlpath = ''; } else {
now creating array substrings $urldata splited '/'
$data = explode( '/', $urldata );
if array $data not empty (otherwise accessing non-existent element raise exception) or lenght of first element equal 0, removes first element array.
while ( !empty( $data ) && strlen( reset( $data ) ) === 0 ) { //notes: php array_shift — shift element off beginning of array array_shift( $data ); }
if array $data not empty (otherwise accessing non-existent element raise exception) or lenght of last element equal 0, removes last element array.
while ( !empty( $data ) && strlen( end( $data ) ) === 0) { array_pop($data); }
array_trim custom function, not sure kind of trimming too
$this->urlbits = $this->array_trim( $data ); }
}
Comments
Post a Comment